-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtries.sublime-snippet
executable file
·62 lines (57 loc) · 1.2 KB
/
tries.sublime-snippet
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
<snippet>
<content><![CDATA[
struct trieNode{
trieNode *node[26];
bool end;
trieNode(){
for(int i = 0 ; i < 26 ; i++){
node[i] = nullptr;
}
end = false;
}
};
class Trie{
public:
trieNode *root , *temp;
int ans;
Trie(){
root = new trieNode;
ans = 0;
}
void add(string s){
temp = root;
for(auto &c : s){
if(temp->node[c - 'a'] == nullptr){
temp->node[c - 'a'] = new trieNode;
}
temp = temp->node[c - 'a'];
}
temp->end = true;
}
void count(trieNode *rot){
if(rot->end) ans++;
for(int i = 0 ; i < 26 ; ++i){
if(rot->node[i] != nullptr){
count(rot->node[i]);
}
}
}
int query(string s){
temp = root;
for(auto &c : s){
if(temp->node[c - 'a'] == nullptr) return 0;
temp = temp->node[c - 'a'];
}
ans = 0;
count(temp);
return ans;
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>tries</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet wiint trigger -->
<scope>source.cpp, source.c++, source.c</scope>
<!-- Optional: Description to show in the menu -->
<description>Trie implementation</description>
</snippet>