-
Notifications
You must be signed in to change notification settings - Fork 247
/
UkkonenSuffixTree.cpp
182 lines (155 loc) · 4.06 KB
/
UkkonenSuffixTree.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/************************************************************************
Suffix Tree. Ukkonen's algorithm using sibling lists — O(N).
This code counts number of different substrings in the string.
Based on problem I from here: http://codeforces.ru/gym/100133
************************************************************************/
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>
using namespace std;
#define root 1
const int MAXN = 105000;
const int inf = 1000 * 1000 * 1000;
struct node {
int from, to, link;
int child, bro;
};
int n, len, nk, pos;
string s;
vector <node> tree;
int active_e, active_node, active_len, needSL, rem;
int add_node(int from, int to) {
nk++;
node temp;
temp.from = from; temp.to = to; temp.link = 0;
temp.child = 0; temp.bro = 0;
tree.push_back(temp);
return nk;
}
void st_init() {
nk = -1;
pos = -1;
rem = active_e = active_len = needSL = 0;
active_node = root;
add_node(-1, -1);
add_node(-1, -1);
}
void addSL(int v) {
if (needSL) tree[needSL].link = v;
needSL = v;
}
int find_edge(int v, int c) {
v = tree[v].child;
while (v) {
if (s[tree[v].from] == c)
return v;
v = tree[v].bro;
}
return 0;
}
void insert_edge(int v, int to) {
int temp = tree[v].child;
tree[v].child = to;
tree[to].bro = temp;
}
void change_edge(int v, int c, int to) {
int next = tree[v].child;
if (s[tree[next].from] == c) {
tree[v].child = to;
tree[to].bro = tree[next].bro;
return;
}
v = next;
while (v) {
next = tree[v].bro;
if (s[tree[next].from] == c) {
tree[v].bro = to;
tree[to].bro = tree[next].bro;
return;
}
v = next;
}
}
bool walk_down(int v) {
int elen = tree[v].to - tree[v].from;
if (tree[v].from + active_len >= tree[v].to) {
active_node = v;
active_len -= elen;
active_e += elen;
return true;
}
return false;
}
int active_edge() {
return s[active_e];
}
void st_insert(int c) {
pos++;
needSL = 0; rem++;
while (rem) {
if (active_len == 0) active_e = pos;
int go = find_edge(active_node, active_edge());
if (go == 0) {
int leaf = add_node(pos, inf);
insert_edge(active_node, leaf);
addSL(active_node);
}
else {
if (walk_down(go))
continue;
if (s[tree[go].from + active_len] == c) {
active_len++;
addSL(active_node);
break;
}
int split = add_node(tree[go].from, tree[go].from + active_len);
int leaf = add_node(pos, inf);
change_edge(active_node, active_edge(), split);
insert_edge(split, go);
insert_edge(split, leaf);
tree[go].from = tree[go].from + active_len;
addSL(split);
}
rem--;
if (active_node == root && active_len) {
active_len--;
active_e = pos - rem + 1;
}
else {
if (tree[active_node].link)
active_node = tree[active_node].link;
else
active_node = root;
}
}
}
int count_diff() {
int result = 0;
for (int i = 2; i <= nk; i++)
result += min(tree[i].to, n) - tree[i].from;
return result;
}
int main() {
freopen("substr.in","r",stdin);
freopen("substr.out","w",stdout);
getline(cin, s);
n = (int) s.length();
st_init();
for (int i = 0; i < n; i++)
st_insert(s[i]);
printf("%d", count_diff());
return 0;
}