manacher马拉车
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 6 char ss[110005],s[220010]; 7 int p[220010]; 8 9 int init() 10 { 11 s[0]='$'; s[1]='#'; 12 int g=2; 13 for(int i=0;ss[i]!='\0';i++) 14 { 15 s[g++]=ss[i]; 16 s[g++]='#'; 17 } 18 s[g]='\0'; 19 return g; //·µ»Ø³¤¶È 20 } 21 int manacher() 22 { 23 int len =init(); 24 int maxlen =-1; 25 26 int id; 27 int mx= 0; 28 29 for(int i=1;i<len;i++) 30 { 31 if(i<mx) p[i]=min( p[2*id-i], mx-i); 32 else p[i]=1; 33 34 while(s[i-p[i]] == s[i+p[i]] ) 35 p[i]++; 36 37 if(i+p[i] > mx) 38 { 39 id=i; 40 mx=p[i]+i; 41 } 42 maxlen=max(maxlen,p[i]-1); 43 } 44 return maxlen; 45 46 } 47 48 int main() 49 { 50 ios::sync_with_stdio(false); 51 while(cin>>ss) 52 { 53 int res=manacher(); 54 cout<<res<<endl; 55 } 56 }