题意:给出一个字符串,要求出一个最长子串的长度,子串满足可以将其分成三部分,第一部分跟第二部分互为回文串,第三部分跟第一部分一样。
做法:
先用求最长回文子串的Manacher算法,求出以第i个点和第i+1个点为中心的回文串长度,记录到数组c中 比如 10 9 8 8 9 10 10 9 8 我们通过运行Manacher求出第i个点和第i+1个点为中心的回文串长度 0 0 6 0 0 6 0 0 0
两个8为中心,10 9 8 8 9 10是个回文串,长度是6。 两个10为中心,8 9 10 10 9 8是个回文串,长度是6。
要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,比如上边的两个字符串,共享 8 9 10这一部分。 也就是说,左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也是一样。 因为我们已经记录下来以第i个点和第i+1个点为中心的回文串长度, 那么问题可以转化成,相距x的两个数a[i],a[i+x],满足a[i]/2>=x 并且 a[i+x]/2>=x,要求x尽量大
这可以用一个set维护,一开始集合为空,将下标跟a数组中的值绑定好,然后按照值降序排序,依次取出a数组中的元素,将其下标放入set中,每取出一个元素,再该集合中二分查找小于等于i+a[i]/2,但最大的元素,更新答案ans。 然后查找集合中大于等于i-a[i]/2,但最小的元素,更新答案ans。
因为排序后,每次要丢元素i进去的时候,若集合中的元素能够被i触及,那么肯定该元素一定能触及i。
答案就是3*ans
时间复杂度是nlogn
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
const int MAXN=110010;
int Ma[MAXN*2],Mp[MAXN*2],s[MAXN];
void Manacher(int len)
{
int l=0;
Ma[l++]=-1;
Ma[l++]=-2;
for(int i=0;i<len;i++)
{
Ma[l++]=s[i];
Ma[l++]=-2;
}
Ma[l]=0;
int mx=0,id=0;
for(int i=0;i<l;i++)
{
Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1;
while(Ma[i+Mp[i]]==Ma[i-Mp[i]])
Mp[i]++;
if(i+Mp[i]>mx)
{
mx=i+Mp[i];
id=i;
}
}
}
struct A
{
int id,val;
bool operator <(A one)const
{
return val!=one.val?val<one.val:id<one.id;
}
}a[MAXN];
void create(int len)
{
Manacher(len);
len=2*len+2;
for(int i=3;i<len;i+=2)
{
int id=i/2-1;
a[id].id=id;
a[id].val=Mp[i]-1;
}
}
int work(int len)
{
int ans=0;
sort(a,a+len);
set<int>st;
set<int>::iterator it;
for(int i=len-1;i>-1;i--)
{
if(a[i].val==0)
break;
int t=a[i].id+a[i].val/2;
it=st.upper_bound(t);
if(it!=st.begin())
{
it--;
if(*it<=t&&*it>a[i].id)
ans=max(ans,*it-a[i].id);
}
t=a[i].id-a[i].val/2;
it=st.lower_bound(t);
if(it!=st.end()&&*it<a[i].id)
ans=max(ans,a[i].id-*it);
st.insert(a[i].id);
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
for(int cs=1;cs<=T;cs++)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",s+i);
create(n);
printf("Case #%d: %d\n",cs,3*work(n));
}
return 0;
}
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1419 Accepted Submission(s): 510
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.
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.
We guarantee that the sum of all answers is less than 800000.
1 10 2 3 4 4 3 2 2 3 4 4
Case #1: 9