namacher裸题++
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <iostream> 6 #define LL long long 7 using namespace std; 8 9 const int maxn=2333333; 10 11 char s[maxn<<1],a[maxn]; 12 int len[maxn<<1]; 13 14 int manachar(char *p) 15 { 16 int l=0,n=strlen(p); 17 s[l++]='$'; s[l++]='#'; 18 for (int i=0; i<n; i++) s[l++]=p[i],s[l++]='#'; 19 s[l++]='&'; s[l]=0; 20 int mx=0,pos=0,ans=0; 21 for (int i=1; i<l; i++) 22 { 23 if (mx>i) len[i]=min(len[pos*2-i],mx-i); else len[i]=1; 24 while (s[i+len[i]]==s[i-len[i]]) len[i]++; 25 ans=max(ans,len[i]); 26 if (len[i]+i>mx) mx=i+len[i],pos=i; 27 } 28 return ans-1; 29 } 30 31 int main() 32 { 33 int T=0; 34 while (scanf("%s",a)!=EOF) 35 { 36 if (a[0]=='E') break; 37 printf("Case %d: %d ",++T,manachar(a)); 38 } 39 return 0; 40 }