1. 程式人生 > >BZOJ 3790: 神奇項鍊

BZOJ 3790: 神奇項鍊

manacher一下,然後就變成經典的線段覆蓋了(我打了貪心的那種)

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+3,mod=1e9+7;
struct P{int l,r;}a[N];
bool Cmp(P x,P y){
    return x.l==y.l?x.r>y.r:x.l<y.l;
}
char s[N],c[N]; int n,r[N];
void manacher(){
    int mx=0,id;
    for(int i=1;i<=n;++i){
        if
(mx>i)r[i]=min(r[id*2-i],mx-i); else r[i]=1; while(i+r[i]<=n && s[i-r[i]]==s[i+r[i]])++r[i]; --r[i]; if(i+r[i]>mx)mx=i+r[i],id=i; } } int main(){ while(~scanf("%s",c+1)){ n=strlen(c+1); int m=0,i,ans=0; for(i=1;i<=n;++i)s[++m]='#',s[++m]=c[i]; s[++m]='#'
,n=m; manacher(); for(i=1;i<=n;++i)a[i]=(P){i-r[i],i+r[i]}; sort(a+1,a+1+n,Cmp); i=1,m=0; while(1){ int mx=0; for(;a[i].l<=m+1 && i<=n;++i)mx=max(mx,a[i].r); m=mx,++ans; if(m==n)break; } printf
("%d\n",ans-1); } return 0; }