Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5765 Accepted Submission(s): 1957
aaaa abab
4 3
#include"iostream" #include"cstdio" #include"cstring" using namespace std; const int MAXN=1000000+100; char str1[MAXN*2],str2[MAXN*2];//待处理字符串 int num[MAXN*2]; //将str1变成str2,如abab变成$#a#b#a#b# void init() { int i,id; str2[0]='$'; str2[1]='#'; for(i=0,id=2;str1[i];i++,id+=2) { str2[id]=str1[i]; str2[id+1]='#'; } str2[id]=0; } //Manacher算法求最长回文子串,时间复杂度为O(N) int Manacher() { int i,ans=0,MxR=0,pos; for(i=1;str2[i];i++) { if(MxR>i)num[i]=num[pos*2-i]<(MxR-i)?num[pos*2-i]:(MxR-i); else num[i]=1; while(str2[i+num[i]]==str2[i-num[i]]) num[i]++; if(num[i]+i>MxR) { MxR=num[i]+i; pos=i; } if(ans<num[i]) ans=num[i]; } return ans-1; } int main() { int ans,tag=1; while(~scanf("%s",str1)) { // if(strcmp(str1,"END")==0)break; init(); ans=Manacher(); printf("%d\n",ans); // printf("Case %d: %d\n",tag++,ans); } return 0; }