Link:http://acm.hdu.edu.cn/showproblem.php?pid=5371
Hotaru's problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1683 Accepted Submission(s): 605
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
Source
AC code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#define LL long long
#define MAXN 100005
using namespace std;
int s1[MAXN*2],s2[MAXN*2];//s1为原字符串,s2为转换后的字符串,注意这里要改成int,char会TLE!!!
int p[MAXN*2];//p[i]表示以s2[i]为中心(包括s2[i])的回文半径,对应s1的回文长度
int len,n;
int a[MAXN*2];
int l,r;//最长回文子串对应原串s1中的位置:l = (i - p[i])/2; r = (i + p[i])/2 - 2;
void pre()
{
int i,j,k;
//len=strlen(s1);
len=n;
s2[0]=-2;
s2[1]=-1;
for(i=0;i<len;i++)
{
s2[i*2+2]=s1[i];
s2[i*2+3]=-1;
}
len=len*2+2;
s2[len]=0;
}
void Manacher()
{
int i;
int mx=0;
int id;
int ans=0;
for(i=1;i<len;i++)
{
if(mx>i)
p[i]=min(p[2*id-i],mx-i);
else
p[i]=1;
for(;s2[i+p[i]]==s2[i-p[i]];p[i]++);
if(ans<p[i])
{
ans=p[i];
// l = (i - p[i])/2;//最长回文子串对应原串s1中的左位置
// r = (i + p[i])/2 - 2;//最长回文子串对应原串s1中的右位置
}
//ans=max(ans,p[i]);
if(p[i]+i>mx)
{
mx=p[i]+i;
id=i;
}
}
//return ans-1;//返回最长回文子串长度
}
int main()
{
int t,i,j,k,an;
scanf("%d",&t);
int cas=0;
while(t--)
{
cas++;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
s1[i]=a[i];
}
memset(p,0,sizeof(p));
pre();
Manacher();
an=0;
for(i=3;i<len;i+=2)
{
if(p[i]-1>an)
{
int rr=p[i]-1;
while(rr>an&&p[i+rr]<rr)
rr--;
an=max(an,rr);
}
}
printf("Case #%d: %d\n",cas,an/2*3);
}
return 0;
}