Hotaru's problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4070 Accepted Submission(s): 1329
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
题意:给定一个数字串,求一个满足下列条件的串:串可以分为三个部分,第一部分和第二部分对称,第二部分和第三部分对称
思路:manecher后,遍历每个位置将其当做第一部分和第二部分的界线,判断后面第二和第三部分是否满足条件
用max函数竟然就超时了...
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <cstring>
#include <queue>
#include <algorithm>
#define ll long long
#define max_ 201000
#define inf 0x3f3f3f3f
#define mod 1000000007
using namespace std;
int n;
int ss[max_],s[max_*2];
int p[max_*2],casnum=1;
int main(int argc, char const *argv[]) {
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&ss[i]);
s[0]=s[1]=-1;
for(int i=0;i<n;i++)
{
s[i+i+2]=ss[i];
s[i+i+3]=-1;
}
int mx=0,id=0,l=2*n+2;
for(int i=0;i<l;i++)
{
if(i<mx)
p[i]=min(p[2*id-i],mx-i);
else
p[i]=1;
while(s[i+p[i]]==s[i-p[i]])
p[i]++;
if(i+p[i]>mx)
{
mx=i+p[i];
id=i;
}
}
int maxx=-1;
for(int i=3;i<l;i+=2)
{
if(p[i]>maxx)
{
int k=p[i];
while(k>maxx&&p[i+k-1]<k)
k-=2;
maxx=maxx>k?maxx:k;
}
}
printf("Case #%d: %d\n",casnum++,(maxx-1)/2*3);
}
return 0;
}