【BZOJ3790】—神奇项链(Manacher+贪心)
先求出极大回文串
考虑相当于就是有很多条线段,要选尽可能少的覆盖整个区间
贪心即可
#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
const int N=150005;
char s[N],a[N];
int n;
int p[N],mp[N],f[N];
struct node{
int l,r;
friend inline bool operator <(const node &a,const node &b){
return a.l<b.l;
}
}o[N];
int tot,ans;
int main(){
while(scanf("%s",s+1)!=EOF){
int len=strlen(s+1);
n=0;
for(int i=1;i<=len;i++)a[++n]='#',a[++n]=s[i];
a[++n]='#';
int mx=1,mid=1;
for(int i=1;i<=n;i++){
if(i<mx)p[i]=min(mx-i,p[2*mid-i]);
else p[i]=1;
while(a[i+p[i]]==a[i-p[i]])p[i]++;
mx=max(mx,i+p[i]);
}
tot=1;
for(int i=1;i<=n;i++)o[i].l=i-p[i]+1,o[i].r=i+p[i]-1;
sort(o+1,o+n+1);
mx=1,ans=0;
while(mx<n){
int res=0;
while(tot<=n&&o[tot].l<=mx)res=max(res,o[tot].r),tot++;
ans++,mx=res;
}
cout<<ans-1<<'\n';
}
}