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
Author
UESTC
Source
Recommend
wange2014
分析:我们先用马拉车求出以每个字符间隙向左右延展的回文串最大半长,设第i个间隙的半长为f[i],那么我们要求的其实就是满足存在f[i+x] >= x && f[i] >= x 的最大的x.
我们考虑从左向右枚举i,并将f[i]减i,这样相当于每次询问[i+1,2*i+f[i]]这段区间中所有大于等于i的值的下标的最大值,因为i是单调递增的,所以我们可以把f[i]排序后用线段树在区间上更新下标,复杂度nlogn.
#include<bits/stdc++.h>
#define N 100005
using namespace std;
typedef pair<int,int> pii;
pii g[N];
int n,m,M,T,Case,ans,s[N],f[N],str[N*2],p[N*2],val[N*4];
void manacher(int n)
{
int i;
for(i = 1;i <= n;i++) str[i*2] = s[i],str[i*2+1] = -1;
str[0] = -2,str[1] = -1;
int up = 2*i;
int res = 0,k = 0,maxk = 0;
for(int i = 2;i < up;i++)
{
p[i] = i < maxk ? min(maxk - i,p[2*k-i]) : 1;
while(str[i-p[i]] == str[i+p[i]]) p[i]++;
if(p[i] + i > maxk) k = i,maxk = i + p[i];
}
for(int i = 3;i < up;i += 2) f[(i+1)/2-1] = (p[i] - 1)/2;
}
void Update(int p, int v) //val空间开四倍
{
p += M;
val[p] = v;
while(p > 1)
{
p >>= 1;
val[p] = max(val[p<<1], val[p<<1|1]);
}
}
int Query(int l, int r) //[l, r]
{
int ans = 0;
for(l = l + M - 1, r = r + M + 1; l^r^1; l >>= 1, r >>= 1)
{
if(~l&1) ans = max(val[l^1], ans); // l为偶数
if(r&1) ans = max(val[r^1], ans); // r为奇数
}
return ans;
}
int main()
{
scanf("%d",&T);
while(T--)
{
ans = 0;
memset(val,0,sizeof(val));
scanf("%d",&n);
for(int i = 1;i <= n;i++) scanf("%d",&s[i]);
manacher(n);
m = n-1;
for(M = 1; M < m + 2; M <<= 1);
for(int i = 1;i < n;i++) g[i] = make_pair(f[i] - i,i);
sort(g+1,g+n);
int tail = n-1;
for(int i = 1;i < n-1;i++)
{
while(tail && (g[tail].first + i) >= 0)
{
Update(g[tail].second,g[tail].second);
tail--;
}
ans = max(ans,Query(i+1,i+f[i]) - i);
}
printf("Case #%d: %d\n",++Case,ans*3);
}
}