-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie_string.cpp
More file actions
36 lines (32 loc) · 812 Bytes
/
Copy pathtrie_string.cpp
File metadata and controls
36 lines (32 loc) · 812 Bytes
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
/*
-> Trie (for strings)
-> ref: https://www.hackerearth.com/practice/data-structures/advanced-data-structures/trie-keyword-tree/tutorial/
*/
struct TrieNode {
TrieNode *ch[28];
int cnt;
TrieNode() {
for (int i = 0; i < 28; ++i) ch[i] = nullptr;
cnt = 0;
}
};
TrieNode *root = nullptr;
void insertString(string s) {
TrieNode *cur = root;
int len = sz(s);
int c;
for (int i = 0; i < len; ++i) {
c = s[i] - 'A';
if (!cur->ch[c]) cur->ch[c] = new TrieNode();
cur = cur->ch[c];
cur->cnt++;
}
}
void resetNodes(TrieNode *cur) {
for (int i = 0; i < 28; ++i) {
if (cur->ch[i] != nullptr) {
cur->ch[i]->cnt = 0;
resetNodes(cur->ch[i]);
}
}
}