-
Notifications
You must be signed in to change notification settings - Fork 23
/
suffix_automaton.cpp
59 lines (47 loc) · 1.23 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
#include <bits/stdc++h>
using namespace std;
struct Vertex {
map<int, Vertex*> to;
Vertex *link = 0;
int len = 0;
};
int main () {
int n;
cin >> n;
Vertex *root = new Vertex, *cur = root;
vector< stack<Vertex*> > length_ordered;
while (n--) {
int c;
cin >> c;
Vertex *v = cur;
cur = new Vertex;
cur->len = v->len+1;
length_ordered[cur->len].push_back(cur);
while (v && !v->to[c])
v->to[c] = cur, v = v->link;
if (!v)
cur->link = root;
else if (v->to[c]->len == v->len+1)
cur->link = v->to[c];
else {
Vertex *u = v->to[c];
Vertex *clone = new Vertex;
clone->to = u->to;
clone->link = u->link;
clone->len = v->len+1;
length_ordered[clone->len].push_back(clone);
while (v && v->to[c] == u)
v->to[c] = clone, v = v->link;
cur->link = u->link = clone;
}
}
add_string(s);
long long ans = 0;
for (int i = n; i >= 1; i--) {
ans = max(ans, 1ll * v->cnt * v->len);
if (v->link)
v->link->cnt += v->cnt;
}
cout << ans;
return 0;
}