-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.sublime-snippet
55 lines (46 loc) · 962 Bytes
/
trie.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
<snippet>
<content><![CDATA[
class trie {
public:
int cnt;
trie* next[ALPHABET_SIZE];
trie() {
cnt = 0;
for (int i = 0; i < ALPHABET_SIZE; i++)
{
next[i] = nullptr;
}
}
void insert(string S) {
int N = S.length();
trie* ptr = this;
for (int i = 0; i < N; i++) {
char ch = S[i];
int ind = ch - 'a';
if (ptr->next[ind] == nullptr)
ptr->next[ind] = new trie;
ptr = ptr->next[ind];
ptr->cnt++;
}
}
int count(string S) {
int ans = 0;
int N = S.length();
trie* ptr = this;
for (int i = 0; i < N; i++) {
char ch = S[i];
int ind = ch - 'a';
if (ptr->next[ind] == nullptr)
return ans;
ptr = ptr->next[ind];
ans += ptr->cnt;
}
return ans;
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>trie</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>