-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsuff_automata.cpp
More file actions
47 lines (44 loc) · 1.19 KB
/
suff_automata.cpp
File metadata and controls
47 lines (44 loc) · 1.19 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
// ************** Construction of suffix automata in O(n) **************
const int maxl = 1 << 17;
const int maxn = 1 << 18;
const int maxa = 53;
char s[maxl];
struct node {
int next[maxa], suff, len, end;
} nodes[maxn];
int root, cnt;
inline int new_node(int len) {
memset(nodes + cnt, -1, sizeof(node));
nodes[cnt].len = len;
nodes[cnt].end = 0;
return cnt++;
}
void extend(char c, int &last) {
int nlast = new_node(nodes[last].len + 1), p;
for (p = last; p >= 0 && nodes[p].next[numc(c)] == -1; p = nodes[p].suff)
nodes[p].next[numc(c)] = nlast;
last = nlast;
if (p < 0) {
nodes[nlast].suff = root;
return;
}
int q = nodes[p].next[numc(c)];
if (nodes[q].len == nodes[p].len + 1) {
nodes[nlast].suff = q;
return;
}
int nq = new_node(nodes[p].len + 1);
memcpy(nodes[nq].next, nodes[q].next, sizeof(nodes[q].next));
nodes[nlast].suff = nq;
nodes[nq].suff = nodes[q].suff;
nodes[q].suff = nq;
for ( ; p >= 0 && nodes[p].next[numc(c)] == q; p = nodes[p].suff)
nodes[p].next[numc(c)] = nq;
}
void create_automata() {
int l = strlen(s), i, last;
cnt = 0;
last = root = new_node(0);
for (i = 0; i < l; ++i) extend(s[i], last);
for (i = last; i >= 0; i = nodes[i].suff) nodes[i].end = 1;
}