-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuffix_tree.cpp
81 lines (75 loc) · 1.5 KB
/
suffix_tree.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
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
using namespace std;
#define fpos adla
const int inf = 1e9;
const int maxn = 1e4;
char s[maxn];
map<int, int> to[maxn];
int len[maxn], fpos[maxn], link[maxn];
int node, pos;
int sz = 1, n = 0;
int make_node(int _pos, int _len)
{
fpos[sz] = _pos;
len [sz] = _len;
return sz++;
}
void go_edge()
{
while(pos > len[to[node][s[n - pos]]])
{
node = to[node][s[n - pos]];
pos -= len[node];
}
}
void add_letter(int c)
{
s[n++] = c;
pos++;
int last = 0;
while(pos > 0)
{
go_edge();
int edge = s[n - pos];
int &v = to[node][edge];
int t = s[fpos[v] + pos - 1];
if(v == 0)
{
v = make_node(n - pos, inf);
link[last] = node;
last = 0;
}
else if(t == c)
{
link[last] = node;
return;
}
else
{
int u = make_node(fpos[v], pos - 1);
to[u][c] = make_node(n - 1, inf);
to[u][t] = v;
fpos[v] += pos - 1;
len [v] -= pos - 1;
v = u;
link[last] = u;
last = u;
}
if(node == 0)
pos--;
else
node = link[node];
}
}
int main()
{
len[0] = inf;
string s;
cin >> s;
int ans = 0;
for(int i = 0; i < s.size(); i++)
add_letter(s[i]);
for(int i = 1; i < sz; i++)
ans += min((int)s.size() - fpos[i], len[i]);
cout << ans << "\n";
}