#include<iostream> #include<cstring> #include<cstdio> using namespace std; const int N=220010; using namespace std; char str[N]; char tmp[N]; int len[N]; int init(){ tmp[0]=‘$‘; int i=0,nlen=1; while(str[i]!=‘\0‘){ tmp[nlen++]=‘#‘; tmp[nlen++]=str[i]; i++; } tmp[nlen++]=‘#‘; tmp[nlen++]=‘$‘; tmp[nlen]=0; return nlen; } int Manacher(int x){ int mx=0,ans=1,pos=0; for(int i=0;i<x;i++){ len[i]=mx>i?min(mx-i,len[2*pos-i]):1; while(tmp[i-len[i]]==tmp[i+len[i]])len[i]++; if(len[i]+i>mx){ mx=len[i]+i; pos=i; } ans=max(ans,len[i]); } return ans-1; } int main(){ while(~scanf("%s",str)){ memset(len,0,sizeof(len)); int x=init(); printf("%d\n",Manacher(x)); } return 0; }
原文地址:https://www.cnblogs.com/Mrleon/p/8419224.html
时间: 2024-11-08 21:04:18