BZOJ4974(给Next求最小字典序原串)

输入给出了最小循环节长度,暗示next数组。

然后自己按照自己的kmp板子逆着来一遍就好。

 1 const int maxn = 1e5 + 5;
 2 int n, a, Next[maxn];
 3 char str[maxn];
 4 bool mark[26];
 5 
 6 int main() {
 7     read(n);
 8     rep(i, 1, n) {
 9         read(a);
10         Next[i] = i - a;
11     }
12     str[1] = 'a';
13     for (int i = 2, j = 0; i <= n; j = Next[i++]) {
14         if (Next[i] == j + 1) {
15             str[i] = str[j + 1];
16         } else {
17             init(mark, 0);
18             do {
19                 mark[str[j + 1] - 'a'] = true;
20                 if (!j)    break;
21                 j = Next[j];
22             } while (Next[i] != j + 1);
23             rep(k, 0, 25) if (!mark[k]) {
24                 str[i] = 'a' + k;
25                 break;
26             }
27         }
28     }
29     printf("%s
", str + 1);
30     return 0;
31 }
原文地址:https://www.cnblogs.com/AlphaWA/p/10577339.html