bzoj2565: 最长双回文串
这题裸题马拉车,跑完以后,找井就可以了。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; int p[210000],l[210000],r[210000]; char sc[110000],ss[210000]; int main() { scanf("%s",sc+1);int len=strlen(sc+1); for(int i=1;i<=len;i++) ss[2*i-1]='#', ss[2*i]=sc[i]; ss[2*len+1]='#';len=2*len+1; int k=0;p[k]=0; for(int i=1;i<=len;i++) { int Lk=k-p[k]+1,Rk=k+p[k]-1; int j=k-(i-k); if(Lk<=j)p[i]=min(p[j],j-Lk+1); else p[i]=0; while(ss[i+p[i]]==ss[i-p[i]]&&i-p[i]>0&&i+p[i]<=len)p[i]++; if(i+p[i]-1>Rk)k=i; } int mx=1; for(int i=1;i<=len;i++) while(mx<=i+p[i])l[mx++]=i; mx=len; for(int i=len;i>=1;i--) while(mx>=i-p[i])r[mx--]=i; int ans=0; for(int i=1;i<=len;i++) if(ss[i]=='#')ans=max(ans,(i-l[i])*2+(r[i]-i)*2+1); printf("%d\n",ans/2); return 0; }
pain and happy in the cruel world.