forked from Priyansh19077/CP-Templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie.cpp
28 lines (28 loc) · 742 Bytes
/
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
const int letters = 26;
struct Node {
vector<int> next_node;
int is_a_leaf = 0;
Node() {
next_node.resize(letters);
fill(begin(next_node), end(next_node), -1);
}
};
struct Trie{
vector<Node> trie_tree;
Trie(){
trie_tree.emplace_back();
}
void add_string(string &a) {
int root_here = 0;
for(char c : a){
int index = c - 'a';
if(trie_tree[root_here].next_node[index] == -1) {
trie_tree[root_here].next_node[index] = sz(trie_tree);
trie_tree.emplace_back();
}
root_here = trie_tree[root_here].next_node[index];
}
trie_tree[root_here].is_a_leaf++;
}
};
Trie t = Trie();