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

浅谈\(KMP\)https://www.cnblogs.com/AKMer/p/10438148.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=4974

首先\(per_i=i-nxt_i\),然后我们可以根据\(per_i\)求出\(nxt_i\)

其次,我们再进行一遍求\(nxt\)的过程,一位一位的确定这个字符串。

因为\([1,i-1]\)都已经确定了,所以根据\(nxt_i\)我可以知道第\(i\)位与哪些位失配了,并且最后是否与某一位匹配上了。

如果与某一位匹配上了,那就直接赋值就行。否则就在失配的字符之外找字典序最小的填到第\(i\)位。

时间复杂度:\(O(n)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <cstring>
using namespace std;

const int maxn=1e6+5;

int n;
bool bo[26];
char s[maxn];
int nxt[maxn];

int read() {
	int x=0,f=1;char ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
	return x*f;
}

int main() {
	n=read();s[1]='a';
	for(int i=1;i<=n;i++)
		nxt[i]=i-read();
	for(int j=0,i=2;i<=n;i++) {
		memset(bo,0,sizeof(bo));
		while(j&&j>nxt[i])bo[s[j+1]-'a']=1,j=nxt[j];
		if(j+1==nxt[i])j++;else bo[s[j+1]-'a']=1;
		if(j)s[i]=s[j];
		else {
			for(int k=0;k<26;k++)
				if(!bo[k]) {s[i]='a'+k;break;}
		}
	}
	printf("%s",s+1);
	return 0;
}
posted @ 2019-02-27 16:38  AKMer  阅读(250)  评论(0编辑  收藏  举报