-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.cpp
75 lines (75 loc) · 2.19 KB
/
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
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
#include<iostream>
using namespace std;
const int sz=2;
class trie{
private:
struct trieNode{
trieNode* children[sz];
trieNode(){
for(int i=0;i<sz;i++){
children[i]=NULL;
}
}
~trieNode(){
for(int i=0;i<sz;i++){
delete children[i];
children[i]=NULL;
}
}
};
trieNode* root;
public:
trie(){
root = new trieNode();
}
void insert(unsigned int n){
trieNode* temp = root;
unsigned int one =1;int cnt=31;
while(cnt>=0){
unsigned int n2=(n&(one<<cnt))>>cnt;
if(temp->children[n2]==NULL){
temp->children[n2] = new trieNode();
}
temp=temp->children[n2];
cnt--;
}
}
int getMax(int n){
trieNode* temp = root;
int curr=0;
int cnt=31;
unsigned int one =1;
while(temp!=NULL){
unsigned int n3=(n&(one<<cnt))>>cnt;
if(temp->children[!n3]!=NULL){
curr=curr|((!n3)<<cnt);
temp = temp->children[!n3];
}
else if(temp->children[n3]!=NULL){
curr=curr|(n3<<cnt);
temp = temp->children[n3];
}
else{
break;
}
cnt--;
}
//cout<<curr<<"\n";
return curr^n;
}
};
int main(){
int n;
cin>>n;
int mx=0;
trie trieTree;
for(int i=0;i<n;i++){
int a;
cin>>a;
if(i>0)
mx = max(mx,trieTree.getMax(a));
trieTree.insert(a);
}
cout<<mx;
return 0;
}