Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2189 Accepted Submission(s): 774
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.
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.
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.
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<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<string> #include<algorithm> 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<n;i++){ scanf("%d",&a[i]); } if(n<3){ printf("0\n");continue; } b[0]=-2; b[1]=-1; for(i=0;i<n;i++){ b[i*2+2]=a[i]; b[i*2+3]=-1; } n=2*n+2;mx=0; for(i=0;i<n;i++){ if(i<mx){ p[i]=min(p[idx*2-i],mx-i); } else p[i]=1; while(b[i-p[i]]==b[i+p[i]]){ p[i]++; } if(mx<i+p[i]){ mx=i+p[i]; idx=i; } } maxx=0; for(i=3;i<n;i+=2){ for(j=p[i]-1;j>=1;j-=2){ if(j<maxx)break; if(i+j-p[i+j]+1<=i){ maxx=max(maxx,j);break; } } } num1++; printf("Case #%d: ",num1); printf("%d\n",maxx/2*3); } return 0; }