Hotaru's problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4234 Accepted Submission(s): 1384
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
Author
UESTC
思路:
代码:(最后一段需要仔细品味)
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000*3;
int s[maxn],str[maxn],p[maxn];
int len1,len2;
void init()
{
str[0]=-2;
str[1]=0;
for(int i=0;i<len1;i++){
str[i*2+2]=s[i];
str[i*2+3]=0;
}
len2=len1*2+2;
str[len2]=-3;
}
void manacher()
{
init();
int id=0,mx=0;
for(int i=1;i<len2;i++){
if(mx>i)
p[i]=min(p[2*id-i],mx-i);
else
p[i]=1;
for(;str[i+p[i]]==str[i-p[i]];p[i]++);
if(p[i]+i>mx){
mx=p[i]+i;
id=i;
}
}
}
int main()
{
int t;
scanf("%d",&t);
int cas=0;
while(t--){
cas++;
scanf("%d",&len1);
for(int i=0;i<len1;i++){
scanf("%d",&s[i]);
}
manacher();
int an=0;
for(int i=3;i<len2;i+=2)
///之所以从i==3开始,并且每次加2,是因为这个位置相当于我们加的井号,也就是数字0,这样保证了求出的回文串是偶串。(就是中间两个对称)
{
if(p[i]-1>an)///如果此时回文串长度大于当前最大值
{
int rr=p[i]-1;
while( rr>an && p[i+rr]-1-rr<0 )///这个地方要仔细品味!!!p【i】的意思是以i位置为中心的回文串的长度的一半加上中间的。去掉我们自己添加的实际上的长度就是p【i】-1
rr--;
an=max(an,rr);
}
}
printf("Case #%d: %d\n",cas,an/2*3);
}
}
Source
2015 Multi-University Training Contest 7
Recommend
wange2014 | We have carefully selected several similar problems for you: 6343 6342 6341 6340 6339