用manacher搞一搞,然后需要以下区间覆盖的贪心就好了。
#include<bits/stdc++.h> using namespace std; char s1[110000],s2[110000]; int n,len,p[110000],lp[110000],rp[110000]; struct one { int x,y; }; one e[110000]; void manacher() { memset(p,0,sizeof(p)); int mx=0,id; for(int i=1;i<len;i++) { if(mx>i)p[i]=min(p[id*2-i],mx-i); else p[i]=1; while(s2[i+p[i]]==s2[i-p[i]]) p[i]++; if(i+p[i]>mx) mx=i+p[i],id=i; } } bool cmp(one aa,one bb) {return aa.x<bb.x;} int main() { //freopen("xf.in","r",stdin); //freopen("xf.out","w",stdout); while(~scanf("%s",s1)) { memset(lp,0,sizeof(lp)); memset(rp,0,sizeof(rp)); s2[0]='$'; n=strlen(s1); len=1; for(int i=0;i<n;i++) { s2[len++]='#'; s2[len++]=s1[i]; } s2[len++]='#'; manacher(); for(int i=1;i<len;i++) { e[i].x=i-p[i]+1; e[i].y=i+p[i]-1; } sort(e+1,e+len,cmp); int l=0,r=0,ans=1; for(int i=1;i<len;i++) { if(e[i].x<=rp[ans-1]+1) { rp[ans]=max(e[i].y,rp[ans]); if(rp[ans]==len-1)break; } else { ans++; rp[ans]=e[i].y; } } printf("%d ",ans-1); } return 0; }