KMP
一个字符串的最小循环节长度为 n − n e x t [ n ] n-next[n] n−next[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;
}