Hotaru’s problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1975 Accepted Submission(s): 703
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算法,注意最后比较的时候用Max记录最长半径能减少比较次数。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
//#define LOCAL
using namespace std;
int a[200020];
int p[200010];
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
#endif // LOCAL
int N;
int CNT = 1;
scanf("%d",&N);
while(N--){
int n;
scanf("%d",&n);
memset(a,0x3f,sizeof(a));
a[0] = -1;
int i,j;
for(i = 2;i<=2*n;i+=2)
scanf("%d",&a[i]);
int mx = -0x3f3f3f3f;
int id;
for(i = 1;i<=2*n+1;i+=2){
if(mx>i)
p[i] = min(p[id*2-i],mx-i);
else
p[i] = 1;
while(a[i-p[i]] == a[i+p[i]])
p[i]+=2;
if(p[i]+i>mx){
mx = p[i]+i;
id = i;
}
}
int Max = 1;
for(i = 1;i<=2*n;i+=2){
for(j = Max;j<=p[i];j+=2){
if(p[i+j-1]>=j)
Max = j;
}
}
Max/=2;
Max*=3;
printf("Case #%d: %d\n",CNT++,Max);
}
return 0;
}