-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrie
84 lines (72 loc) · 1.64 KB
/
Trie
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
/*
Practice:
Problem : https://codeforces.com/contest/842/problem/D
Solution: https://codeforces.com/contest/842/submission/152356883
Problem : https://codeforces.com/problemset/problem/282/E
Solution: https://codeforces.com/contest/282/submission/152987420
Problem : https://codeforces.com/problemset/problem/633/C
Solution: https://codeforces.com/problemset/submission/633/276810614
/**/
// for distinct 26 characters.
struct node{
node *ch[26] ;
bool isEnd;
node()
{
isEnd = false;
for(int i=0; i<26; i++)
{
ch[i] = NULL;
}
}
}*Root = new node();
void Insert(string s){
node *curr=Root ;
int i=0;
for(char x:s)
{
i++;
if(curr->ch[x-'a']==NULL)
{
curr->ch[x-'a']=new node() ;
}
curr=curr->ch[x-'a'];
if(i==s.size())
{
curr->isEnd = true;
break;
}
}
}
-----------------------------------
// for binary string 0/1.
struct node{
node *ch[2] ;
int cnt ;
node()
{
ch[0]=ch[1]=NULL, cnt=0 ;
}
}*root ;
void insert(int x){
node *curr=root ;
for(int i=20,bit; i>=0; --i){
bit=(x>>i)&1 ;
if(curr->ch[bit]==NULL)
curr->ch[bit]=new node() ;
curr=curr->ch[bit], curr->cnt++ ;
}
}
int mex(int x){
node *curr=root ; int num=0 ;
for(int i=20,bit; i>=0; --i){
bit=(x>>i)&1 ;
if(curr->ch[bit]!=NULL and curr->ch[bit]->cnt==(1<<i))
curr=curr->ch[bit^1] , num^=(1<<i) ;
else
curr=curr->ch[bit] ;
if(curr==NULL)
return num ;
}
return num ;
}