-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_trie.cpp
102 lines (76 loc) · 2.07 KB
/
binary_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// BOJ 13505 두 수 XOR
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
struct Node {
int zero = -1;
int one = -1;
};
struct Trie {
vector<Node> nodes;
Trie() { nodes.push_back(Node()); }
void insert(int cur, int x, int bit) {
if (bit == 0)
return;
if (x & bit) {
if (nodes[cur].one == -1) {
nodes[cur].one = nodes.sz;
nodes.push_back(Node());
}
insert(nodes[cur].one, x, bit >> 1);
} else {
if (nodes[cur].zero == -1) {
nodes[cur].zero = nodes.sz;
nodes.push_back(Node());
}
insert(nodes[cur].zero, x, bit >> 1);
}
}
int minimum(int cur, int x, int bit) {
if (bit == 0)
return 0;
if (x & bit) {
if (nodes[cur].one == -1)
return bit + minimum(nodes[cur].zero, x, bit >> 1);
return minimum(nodes[cur].one, x, bit >> 1);
}
if (nodes[cur].zero == -1)
return bit + minimum(nodes[cur].one, x, bit >> 1);
return minimum(nodes[cur].zero, x, bit >> 1);
}
int maximum(int cur, int x, int bit) {
if (bit == 0)
return 0;
if (x & bit) {
if (nodes[cur].zero == -1)
return maximum(nodes[cur].one, x, bit >> 1);
return bit + maximum(nodes[cur].zero, x, bit >> 1);
}
if (nodes[cur].one == -1)
return maximum(nodes[cur].zero, x, bit >> 1);
return bit + maximum(nodes[cur].one, x, bit >> 1);
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
cin >> v[i];
Trie trie;
int bit = (1 << 29);
for (int x : v)
trie.insert(0, x, bit);
int ans = 0;
for (int x : v)
ans = max(ans, trie.maximum(0, x, bit));
cout << ans;
}