-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.cpp
49 lines (48 loc) · 949 Bytes
/
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
#include<bits/stdc++.h>
using namespace std;
class BiTrie{
public:
BiTrie* node[2];
void insert(int x){
BiTrie* curr = this;
for(int i=30;i>=0;--i){
int y = ((x&(1<<i)) ? 1 : 0);
if(!curr->node[y]) curr->node[y] = new BiTrie();
curr = curr->node[y];
}
}
int search(int x){
BiTrie* curr = this;
for(int i=30;i>=0;--i){
int y = ((x&(1<<i)) ? 1 : 0);
if(!curr->node[y]) return 0;
curr = curr->node[y];
}
return 1;
}
};
class Trie{
public:
Trie* node[26];int end = 0;
void insert(string& s){
Trie* curr = this;
for(int i=0;s[i];++i){
int y = s[i]-'a';
if(!curr->node[y]) curr->node[y] = new Trie();
curr = curr->node[y];
}
curr->end = 1;
}
int search(string& s){
Trie* curr = this;
for(int i=0;s[i];++i){
int y = s[i]-'a';
if(!curr->node[y]) return 0;
curr = curr->node[y];
}
return (curr->end);
}
};
int main(){
BiTrie trie;
}