程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> hdu5371 Hotaru's problem

hdu5371 Hotaru's problem

編輯:關於C++
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2189 Accepted Submission(s): 774



Problem Description Hotaru Ichijou recently is addicated to math problems. Now she is playing with N-sequence.
Let's define N-sequence, which is composed with three parts and satisfied with the following condition:
1. the first part is the same as the thrid part,
2. the first part and the second part are symmetrical.
for example, the sequence 2,3,4,4,3,2,2,3,4 is a N-sequence, which the first part 2,3,4 is the same as the thrid part 2,3,4, the first part 2,3,4 and the second part 4,3,2 are symmetrical.

Give you n positive intergers, your task is to find the largest continuous sub-sequence, which is N-sequence.

Input There are multiple test cases. The first line of input contains an integer T(T<=20), indicating the number of test cases.

For each test case:

the first line of input contains a positive integer N(1<=N<=100000), the length of a given sequence

the second line includes N non-negative integers ,each interger is no larger than 109 , descripting a sequence.

Output Each case contains only one line. Each line should start with “Case #i: ”,with i implying the case number, followed by a integer, the largest length of N-sequence.

We guarantee that the sum of all answers is less than 800000.

Sample Input
1
10
2 3 4 4 3 2 2 3 4 4

Sample Output
Case #1: 9

 

這題可以用Manacher算法做,因為題目要找的是三段(第一段和第二段對稱,第二段和第三段對稱),其實就是兩個連在一起的回文串,我們可以先用Manacher算法初始化各個點的p[i]值(即可以向右延伸的最大距離,包括本身,這時已經加入了-1代替算法中的'#',-2代替算法中的'$'),然後對於每個i,枚舉j(j屬於1~p[i]-1),如果i+j-p[i+j]+1<=i,那麼說明i,j可以分別作為第一、二段的點和第二、三段的點)。

這裡有個優化,因為枚舉時滿足條件的只有'#'(即'-1’),所以我們可以使i,j每次變化2.

 

 

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define maxn 100060
int a[maxn],b[2*maxn],p[2*maxn];
int main()
{
    int n,m,i,j,T,mx,idx,maxx,num1=0;
    scanf(%d,&T);
    while(T--)
    {
        scanf(%d,&n);
        for(i=0;i=1;j-=2){
                if(j

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved