[LOJ129] Lyndon 分解

解法

关于 Lyndon word 的一些结论可以看这个

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = (1<<20)+10;

int n;
char str[maxn];

int main() {
scanf("%s", str+1);
n = int(strlen(str+1));
int s = 1, l = 0, p = 0;
while (s < n) {
l = 1; p = s+1;
for (p = s+1; ; ++p) {
if (str[p] < str[p-l]) {
while (s+l-1 < p) {
printf("%d ", s+l-1);
s += l;
}
break;
} else if (str[p] > str[p-l]) {
l = p-s+1;
}
}
}
if (s <= n) printf("%d ", n);
printf("\n");
return 0;
}