BZOJ4974: [Lydsy1708月赛]字符串大师

KMP

题目传送门

一个字符串的最小循环节长度为 n − n e x t [ n ] n-next[n] nnext[n]。那么这道题相当于给我们 n e x t [ i ] next[i] next[i],要求原串。

当构造 n e x t next next数组时,我们是跳到与当前字符一样的地方并更新 n e x t next next,否则 n e x t next next是不会变的。那么现在就相当于在原串种有一些相等与不相等的限制条件,对于相等的条件直接取,不相等的取能取的最小的即可。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100005
using namespace std;
int n,a[N],nxt[N];
bool f[N][26];
int main(){
	scanf("%d",&n),nxt[0]=-1;
	for (int i=1,x;i<=n;i++)
		scanf("%d",&x),nxt[i]=i-x;
	int l=-1,r=0;
	while (r<n)
		if (nxt[r+1]==l+1){
			if (l!=-1) a[r]=a[l];
			else{
				int now=0;
				while (f[r][now]) now++;
				a[r]=now;
			}
			r++,l++;
		}
		else f[r][a[l]]=true,l=nxt[l];
	for (int i=0;i<n;i++) putchar(a[i]+'a');
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值