【BZOJ3790】—神奇项链(Manacher+贪心)

传送门


M a n a c h e r Manacher 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';
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值