-
Notifications
You must be signed in to change notification settings - Fork 247
/
Trie.cpp
61 lines (48 loc) · 1.36 KB
/
Trie.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
/****************************************************************************
Trie. Builds tree from the set of strings. O(total length of strings)
This code counts number of different substrings in the string.
Based on problem B from here: https://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;
const int ALPH = 26;
const int MAXN = 100500;
struct node {
int go[ALPH];
};
string s;
node trie[MAXN];
int node_num;
int main() {
assert(freopen("unequal.in","r",stdin));
assert(freopen("unequal.out","w",stdout));
getline(cin, s);
for (int i = 0; i < (int) s.length(); i++) {
int cur = 0;
for (int j = i; j < (int) s.length(); j++) {
int ch = s[j] - 'a';
if (trie[cur].go[ch] == 0) {
node_num++;
trie[cur].go[ch] = node_num;
}
cur = trie[cur].go[ch];
}
}
printf("%d\n", node_num);
return 0;
}