bzoj4974 字符串大师 KMP

这个好神啊~

code:

#include <bits/stdc++.h>  
#define N 100040 
#define setIO(s) freopen(s".in","r",stdin) 
using namespace std; 
int nex[N],vis[27]; 
char str[N];           
int main() 
{ 
	// setIO("input"); 
	int n,i,j; 
	scanf("%d",&n);     
	nex[0]=-1; 
	for(i=1;i<=n;++i) 
	{
		scanf("%d",&nex[i]), nex[i]=i-nex[i];     
		if(nex[i]) str[i]=str[nex[i]];           
		else 
		{
			for(j=nex[i-1];~j;j=nex[j]) vis[str[j+1]-'a']=i;    
			for(j=0;j<26;++j) if(vis[j]!=i) break;   
			str[i]=j+'a';   
		}
	} 
	printf("%s\n",str+1); 
	return 0;
}

  

posted @ 2019-10-15 18:19  EM-LGH  阅读(99)  评论(0编辑  收藏  举报