-
Notifications
You must be signed in to change notification settings - Fork 1
/
P3809.c
42 lines (36 loc) · 1.31 KB
/
P3809.c
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
35
36
37
38
39
40
41
42
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAXN 1000006
char s[MAXN];
int sa[MAXN], rk[MAXN << 1], oldrk[MAXN << 1], id[MAXN], cnt[MAXN];
int main() {
scanf("%s", s + 1);
int n = strlen(s + 1);
int m = 300;
int i, w, p;
for (i = 1; i <= n; i++) ++cnt[rk[i] = s[i]];
for (i = 1; i <= m; i++) cnt[i] += cnt[i - 1];
for (i = n; i >= 1; i--) sa[cnt[rk[i]]--] = i;
for (w = 1; w < n; w <<= 1, m = p) {
memset(cnt, 0, sizeof(cnt));
for (i = 1; i <= n; i++) id[i] = sa[i];
for (i = 1; i <= n; i++) ++cnt[rk[id[i] + w]];
for (i = 1; i <= m; i++) cnt[i] += cnt[i - 1];
for (i = n; i >= 1; i--) sa[cnt[rk[id[i] + w]]--] = id[i];
memset(cnt, 0, sizeof(cnt));
for (i = 1; i <= n; i++) id[i] = sa[i];
for (i = 1; i <= n; i++) ++cnt[rk[id[i]]];
for (i = 1; i <= m; i++) cnt[i] += cnt[i - 1];
for (i = n; i >= 1; i--) sa[cnt[rk[id[i]]]--] = id[i];
memcpy(oldrk, rk, sizeof(rk));
for (p = 0, i = 1; i <= n; i++) {
if (oldrk[sa[i]] == oldrk[sa[i - 1]] && oldrk[sa[i] + w] == oldrk[sa[i - 1] + w])
rk[sa[i]] = p;
else
rk[sa[i]] = ++p;
}
}
for (int i = 1; i < n; i++) printf("%d ", sa[i]); printf("%d\n", sa[n]);
exit(0);
}