标签:
1 //经典manachar 2 #include<cstdio> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 #define MAXN 2010000 8 int p[MAXN]; 9 char s1[MAXN],s2[MAXN]; 10 int manachar(int m){ 11 int i,ans=0,mx=-1,id=-1; 12 for(i=1;i<=m;i++){ 13 if(id+mx>=i) p[i]=min(p[id*2-i],id+mx-i); 14 while(i-p[i]-1>=0&&i+p[i]+1<=m&&s2[i-p[i]-1]==s2[i+p[i]+1]) p[i]++; 15 if(i+p[i]>id+mx) 16 mx=p[i],id=i; 17 ans=max(ans,p[i]); 18 } 19 return ans; 20 } 21 void deal(int m){ 22 for(int i=0;i<m;i++) 23 s2[i*2+1]=s1[i],s2[i*2+2]=‘#‘; 24 s2[0]=‘#‘; 25 s2[m*2+1]=‘\0‘; 26 memset(p,0,sizeof(p)); 27 } 28 int main() 29 { 30 int cnt=0; 31 while(scanf("%s",s1)){ 32 if(strcmp(s1,"END")==0)break; 33 int m=strlen(s1); 34 deal(m); 35 printf("Case %d: %d\n",++cnt,manachar(m*2+1)); 36 } 37 }
标签:
原文地址:http://www.cnblogs.com/shenben/p/5459916.html