forked from mateusvrs/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suffix-automaton.cpp
68 lines (63 loc) · 1.39 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class SuffixAutomaton {
public:
struct state {
int len, link;
array<int, 26> next;
};
vector<state> st;
int sz = 0, last;
SuffixAutomaton(const string& s) : st(s.size() << 1) {
sa_init();
for (auto v : s) sa_extend((int)(v - 'a'));
}
void sa_init() {
st[0].len = 0;
st[0].link = -1;
sz++;
last = 0;
}
void sa_extend(int c) {
int cur = sz++;
st[cur].len = st[last].len + 1;
int p = last;
while (p != -1 && !st[p].next[c]) {
st[p].next[c] = cur;
p = st[p].link;
}
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].link = st[q].link;
st[clone].next = st[q].next;
while (p != -1 && st[p].next[c] == q) {
st[p].next[c] = clone;
p = st[p].link;
}
st[q].link = st[cur].link = clone;
}
}
last = cur;
}
// longest common substring O(N)
int lcs(const string& T) {
int v = 0, l = 0, best = 0;
for (int i = 0; i < T.size(); i++) {
while (v && !st[v].next[T[i] - 'a']) {
v = st[v].link;
l = st[v].len;
}
if (st[v].next[T[i] - 'a']) {
v = st[v].next[T[i] - 'a'];
l++;
}
best = max(best, l);
}
return best;
}
};