hdu 3068 Manacher算法
题意:求最长回文串,模板题
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 1000000007 10 const int INF=0x3f3f3f3f; 11 const double eps=1e-5; 12 #define cl(a) memset(a,0,sizeof(a)) 13 #define ts printf("*****\n"); 14 int n,m,tt;const int MAXN=110010; 15 char Ma[MAXN*2]; 16 int Mp[MAXN*2]; 17 void Manacher(char s[],int len) 18 { 19 int l=0; 20 Ma[l++]='$'; 21 Ma[l++]='#'; 22 for(int i=0;i<len;i++) 23 { 24 Ma[l++]=s[i]; 25 Ma[l++]='#'; 26 } 27 Ma[l]=0; 28 int mx=0,id=0; 29 for(int i=0;i<l;i++) 30 { 31 Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1; 32 while(Ma[i+Mp[i]]==Ma[i-Mp[i]])Mp[i]++; 33 if(i+Mp[i]>mx) 34 { 35 mx=i+Mp[i]; 36 id=i; 37 } 38 } 39 } 40 char s[MAXN]; 41 int main() 42 { 43 #ifndef ONLINE_JUDGE 44 freopen("1.in","r",stdin); 45 #endif 46 while(scanf("%s",s)==1) 47 { 48 int len=strlen(s); 49 Manacher(s,len); 50 int ans=0; 51 for(int i=0;i<2*len+2;i++) 52 ans=max(ans,Mp[i]-1); 53 printf("%d\n",ans); 54 } 55 return 0; 56 }