-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path208.trie.cpp
113 lines (105 loc) · 2.62 KB
/
208.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
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
// const int N = 1000010;
// int son[N][26], cnt[N], idx;
class Trie {
private:
int son[100010][26] = {0};
int cnt[100010] = {0};
int idx = 0;
public:
/** Initialize your data structure here. */
Trie() {
// son.memset(son, 0, sizeof(son));
// cnt.memset(cnt, 0, sizeof(cnt));
// idx = 0;
}
/** Inserts a word into the trie. */
void insert(string word) {
int p = 0;
for(int i = 0; i < word.size(); i++){
int s = word[i] - 'a';
if(!son[p][s]){
// 用数组模拟树
// p 代表 root,s 代表 root->0, root->1
// 这样就好理解了
son[p][s] = ++idx;
}
p = son[p][s];
}
cnt[p]++;
}
/** Returns if the word is in the trie. */
bool search(string word) {
int p = 0;
for(int i = 0; i < word.size(); i++){
int s = word[i] - 'a';
if(!son[p][s]){
return false;
}
p = son[p][s];
}
if(cnt[p]){
return true;
}else{
return false;
}
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
int p = 0;
for(int i = 0; i < prefix.size(); i++){
int s = prefix[i] - 'a';
if(!son[p][s]){
return false;
}
p = son[p][s];
}
return true;
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/
class Trie {
private:
bool isEnd;
Trie* next[26];
public:
Trie() {
isEnd = false;
memset(next, 0, sizeof(next));
}
void insert(string word) {
Trie* node = this;
for (char c : word) {
if (node->next[c-'a'] == NULL) {
node->next[c-'a'] = new Trie();
}
node = node->next[c-'a'];
}
node->isEnd = true;
}
bool search(string word) {
Trie* node = this;
for (char c : word) {
node = node->next[c - 'a'];
if (node == NULL) {
return false;
}
}
return node->isEnd;
}
bool startsWith(string prefix) {
Trie* node = this;
for (char c : prefix) {
node = node->next[c-'a'];
if (node == NULL) {
return false;
}
}
return true;
}
};