-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrie.cpp
More file actions
executable file
·33 lines (31 loc) · 965 Bytes
/
Trie.cpp
File metadata and controls
executable file
·33 lines (31 loc) · 965 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
#include <bits/stdc++.h>
using namespace std;
class Trie {
public:
struct Node{
bool eow;
Node* nb[26];
Node():eow(false){
for(int i = 0; i != 26; i++)
nb[i] = nullptr;
}
};
Node *root;
Trie() {
root = new Node();
root->eow = true;
}
void insert(string &word) {
Node *tmp = root;
for(int j = 0, idx ;j < word.size(); j++)
if(tmp->nb[idx = word[j] - 'a'])
//if this node present
tmp = tmp->nb[idx];
else{
//if not
for(int idx; j != word.size(); j++, tmp = tmp->nb[idx])
tmp->nb[idx = word[j] - 'a'] = new Node();
}
tmp->eow = true;
}
};