标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12244 Accepted Submission(s): 4501
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<vector> 7 #define mem(x,y) memset(x,y,sizeof(x)) 8 using namespace std; 9 typedef long long LL; 10 const int INF=0x3f3f3f3f; 11 const int MAXN=250005; 12 int p[MAXN]; 13 char str[MAXN],s[MAXN]; 14 int Manacher(char *s,int len){ 15 mem(p,0); 16 p[0]=p[1]=1; 17 int id=1,mx=1; 18 int ans=0; 19 for(int i=2;i<len;i++){ 20 if(mx>i)p[i]=min(p[2*id-i],mx-i); 21 //找对称轴左侧这个地方和最大值地方的长度大小,找小的 22 else p[i]=1; 23 while(s[i-p[i]]==s[i+p[i]])p[i]++; 24 if(p[i]+i>mx)mx=p[i]+i,id=i;//更新对称轴id,更新右侧匹配的最大值mx 25 ans=max(ans,p[i]); 26 } 27 return ans-1; 28 } 29 int main(){ 30 int flot=0; 31 while(~scanf("%s",str)){ 32 int len=strlen(str); 33 s[0]=‘@‘; 34 for(int i=0;i<len;i++){ 35 s[2*i+1]=‘#‘; 36 s[2*i+2]=str[i]; 37 } 38 s[2*len+1]=‘#‘; 39 // if(flot++)puts(""); 40 printf("%d\n",Manacher(s,2*len+2)); 41 } 42 return 0; 43 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4908954.html