-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuffix_array_nlogn.cpp
More file actions
113 lines (94 loc) · 2.43 KB
/
suffix_array_nlogn.cpp
File metadata and controls
113 lines (94 loc) · 2.43 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
string txt;
int n;
vector<int> pos, sa;
vector<int> cnt, nxt;
vector<bool> bh, b2h;
vector<int> lcp;
void init() {
sa.rz(n);
pos.rz(n);
cnt.rz(n);
nxt.rz(n);
bh.rz(n);
b2h.rz(n);
lcp.rz(n - 1);
}
bool smaller_first_char(int a, int b) {
return txt[a] < txt[b];
}
void buildSA() {
for (int i = 0; i < n; ++i)
sa[i] = i;
sort(sa.begin(), sa.begin() + n, smaller_first_char);
for (int i = 0; i < n; ++i) {
bh[i] = i == 0 || txt[sa[i]] != txt[sa[i - 1]];
b2h[i] = false;
}
for (int h = 1; h < n; h <<= 1) {
int buckets = 0;
for (int i = 0, j; i < n; i = j) {
j = i + 1;
while (j < n && !bh[j])
++j;
nxt[i] = j;
++buckets;
}
if (buckets == n)
break;
for (int i = 0; i < n; i = nxt[i]) {
cnt[i] = 0;
for (int j = i; j < nxt[i]; ++j)
pos[sa[j]] = i;
}
cnt[pos[n - h]]++;
b2h[pos[n - h]] = true;
for (int i = 0; i < n; i = nxt[i]) {
for (int j = i; j < nxt[i]; ++j) {
int s = sa[j] - h;
if (s >= 0) {
int head = pos[s];
pos[s] = head + cnt[head]++;
b2h[pos[s]] = true;
}
}
for (int j = i; j < nxt[i]; ++j) {
int s = sa[j] - h;
if (s >= 0 && b2h[pos[s]]) {
for (int k = pos[s] + 1; !bh[k] && b2h[k]; ++k)
b2h[k] = false;
}
}
}
for (int i = 0; i < n; ++i) {
sa[pos[i]] = i;
bh[i] = bh[i] | b2h[i];
}
}
for (int i = 0; i < n; ++i)
pos[sa[i]] = i;
}
void buildLCP() {
int k = 0;
for (int i = 0; i < n; ++i) {
if (pos[i] == n - 1) {
k = 0;
continue;
}
int j = sa[pos[i] + 1];
while (i + k < n && j + k < n && txt[i + k] == txt[j + k])
k++;
lcp[pos[i]] = k;
if (k)
--k;
}
}
void dispSA() {
cout << "sa: ";
nshow(sa, n);
cout << "pos: ";
nshow(pos, n);
cout << "lcp: ";
nshow(lcp, n - 1);
for (int i = 0; i < n; ++i)
cout << txt.substr(sa[i]) << "\n";
}