-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathsuffix_automaton.cpp
51 lines (48 loc) · 1.15 KB
/
suffix_automaton.cpp
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
struct state
{
int len, link;
map<char, int> next;
};
const int N = 300000 + 5;
struct SA
{
state st[2 * N];
int fpos[2 * N];
int sz, last;
void sa_init()
{
sz = last = 0;
st[0].len = 0;
st[0].link = -1;
++sz;
}
void sa_extend(char c)
{
int cur = sz++;
st[cur].len = st[last].len + 1;
fpos[cur] = st[cur].len - 1;
int p;
for(p = last; p != -1 && !st[p].next.count(c); p = st[p].link)
st[p].next[c] = cur;
if(p == -1)
st[cur].link = 0;
else
{
int q = st[p].next[c];
if(st[p].len + 1 == st[q].len)
st[cur].link = q;
else
{
int clone = sz++;
st[clone].len = st[p].len + 1;
st[clone].next = st[q].next;
st[clone].link = st[q].link;
fpos[clone] = fpos[q];
for(; p != -1 && st[p].next[c] == q; p = st[p].link)
st[p].next[c] = clone;
st[q].link = st[cur].link = clone;
}
}
last = cur;
}
};