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.
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
We guarantee that the sum of all answers is less than 800000.
1 10 2 3 4 4 3 2 2 3 4 4
Case #1: 9
#include<iostream>
#include<stdio.h>
#include<algorithm>
#define Inf 0x3f3f3f3f
using namespace std;
const int N = 100005;
int a[N];
int p[N*2],arr[N*2];
void Manacher(int a[],int n){
arr[0]=Inf;
for(int i=1;i<=n;i++){
arr[i*2-1]=a[i];
arr[i*2]=Inf;
}
arr[n*2+1]=Inf;
int mx=0,id=0;
for(int i=0;i<2*n+1;i++){
p[i]=mx>i?min(p[2*id-i],mx-i):1;
while(arr[i+p[i]]==arr[i-p[i]]) ++p[i];
if(mx<i+p[i]){
mx=i+p[i];
id=i;
}
}
}
int main(){
int T;
scanf("%d",&T);
int cas=0;
while(T--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
Manacher(a,n);
int ans=0;
for(int i=1;i<=2*n+1;i++){
for(int j=p[i]+i-1;j-i>ans;j-=2){//j为以i为中心的回文字符串最后一个字符的位置
if(j-i<=p[j]){//j-i==p[i]-1 即以i为中心的原字符串的长度
ans=max(ans,j-i);
break;
}
}
}
printf("Case #%d: %d\n",++cas,ans/2*3);
}
return 0;
}