aaaa abab
4 3
思路:这里讲得很详细了http://blog.csdn.net/ggggiqnypgjg/article/details/6645824
#include <cstdio> using namespace std; char s[110005],str[220010]; int p[220010]; int main() { int len,i,maxlen,maxindex,ans; while(~scanf("%s",s)) { str[0]=‘$‘; str[1]=‘#‘; for(i=0;s[i];i++) { str[i*2+2]=s[i]; str[i*2+3]=‘#‘; } len=i*2+2; str[len]=‘\0‘; maxlen=0; for(i=1;i<len;i++) { if(maxlen>i) p[i]=p[2*maxindex-i]<maxlen-i?p[2*maxindex-i]:maxlen-i; else p[i]=1; while(str[i-p[i]]==str[i+p[i]]) { p[i]++; } if(p[i]+i>maxlen) { maxlen=p[i]+i; maxindex=i; } } ans=0; for(i=1;i<len;i++) if(ans<p[i]) ans=p[i]; printf("%d\n",ans-1); } }
HDU-3068-最长回文 (Manacher算法),码迷,mamicode.com
原文地址:http://blog.csdn.net/faithdmc/article/details/24658273