标签:
#include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <string> #include <math.h> #include <stdlib.h> using namespace std; int p[maxn]; char s[maxn]; void manacher(char *s){//时间复杂度O(n) int len=strlen(s); for(int i=len;i>=0;i--){ s[i+i+2]=s[i]; s[i+i+1]=‘#‘; } s[0]=‘*‘; int k=1,maxlen=0; for(int i=2;i<len+len+1;i++){ int maxr=k+p[k]-1; p[i]=min(p[2*k-i],max(maxr-i+1,1)); while(s[i-p[i]]==s[i+p[i]])++p[i]; if(i+p[i]>k+p[k])k=i; if(p[i]>maxlen)maxlen=p[i]; } printf("%d\n",maxlen-1); } int main() { while(scanf("%s",s)!=EOF){ manacher(s); } return 0; }
标签:
原文地址:http://www.cnblogs.com/pk28/p/4873556.html