-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathMaximumXOR.cpp
81 lines (75 loc) · 1.33 KB
/
MaximumXOR.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
76
77
78
79
80
81
#include<bits/stdc++.h>
using namespace std;
struct trie{
trie *child[2];
bool is_end;
};
trie *new_node()
{
trie *temp=new trie;
temp->child[0]=NULL;
temp->child[1]=NULL;
temp->is_end=false;
return temp;
}
void insert(trie *root,int x)
{
trie *temp=root;
int index;
for(int i=31;i>=0;i--)
{
if(x&(1<<i))
index=1;
else
index=0;
if(temp->child[index]==NULL)
temp->child[index]=new_node();
temp=temp->child[index];
}
temp->is_end=true;
}
void search_result(trie *root,int x)
{ int cnt=0,index,index_;
trie *temp=root;
for(int i=31;i>=0;i--)
{
if(x&(1<<i))
index=1;
else
index=0;
index_=(index+1)%2;
if(temp->child[index_]!=NULL && temp->is_end==false)
{
cnt=cnt+pow(2,i) ;
temp=temp->child[index_];
}
else {
temp=temp->child[index];
}
}
cout<<cnt<<endl;
}
void max_xor(int vec[],int n,int q[],int t)
{
trie *root=new_node();
for(int i=0;i<n;i++){
//cout<<"insert: "<<q[i]<<endl;;
insert(root,vec[i]);}
for(int i=0;i<t;i++)
{ //cout<<"q["<<i<<"]: "<<q[i]<<"=";
search_result(root,q[i]);
}
}
int main()
{
int n,x,t;
cin>>n;
int vec[n];
for(int i=0;i<n;i++)
cin>>vec[i];
cin>>t;int q[t];
for(int i=0;i<t;i++)
cin>>q[i];
max_xor(vec,n,q,t);
return 0;
}