-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
50 lines (47 loc) · 843 Bytes
/
trie.cpp
File metadata and controls
50 lines (47 loc) · 843 Bytes
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
//God & me // ya mahD adrekni
#include <bits/stdc++.h>
using namespace std;
const int mh=30;
bool stak[mh+1];
int sz;
void insert(string &s){
int pos=0;
for(auto &c:s)
c-='a',pos=t[pos][c]?t[pos][c]:t[pos][c]=sz++;
}
struct trie{
int tr[1000012][2],size=1;
inline void insert(int x){
for(int i=0;i<=mh;i++)
stak[sz++]=x&1,x>>=1;
int pos=0;
while(sz--){
if(!tr[pos][stak[sz]])
tr[pos][stak[sz]]=size++;
pos=tr[pos][stak[sz]];
}
}
inline bool find(int x){
for(int i=0;i<=mh;i++)
stak[sz++]=x&1,x>>=1;
int pos=0;
while(sz--){
if(!tr[pos][stak[sz]])
return sz=0;
pos=tr[pos][stak[sz]];
}
return true;
}
};
trie a;
int q,x;
int main(){
cin>>q;
while(q--){
cin>>x;
cout<<a.find(x)<<endl;
a.insert(x);
}
return 0;
}
//be omide khoda!