-
Notifications
You must be signed in to change notification settings - Fork 0
/
13505_trie.cpp
89 lines (88 loc) · 1.58 KB
/
13505_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
// 210808 #13505 µÎ ¼ö XOR Platinum III
// Trie. Why? -> all number memory. Only bits and check all number O(N*32)
#include <iostream>
#include <vector>
#include <algorithm>
#define all(v) (v).begin(), (v).end()
#define press(v) (v).erase(unique(all(v)), (v).end())
using namespace std;
const int MAX = 100001;
int N, a[MAX], ans;
struct Trie {
Trie* next[2];
int branch;
bool end;
Trie() :branch(0), end(false) {
fill(next, next + 2, nullptr);
}
~Trie() {
for (int i = 0; i < 2; i++) {
if (next[i])
delete next[i];
}
}
void insert(string& s, int idx) {
if (idx >= s.length()) {
end = true;
return;
}
int x = s[idx] - '0';
if (!next[x]) {
next[x] = new Trie();
branch++;
}
next[x]->insert(s, idx + 1);
}
void find(string& s, int idx) {
if (end)
return;
int x = s[idx] - '0';
if (next[1 - x]) {
s[idx] = '1';
next[1 - x]->find(s, idx + 1);
}
else {
s[idx] = '0';
next[x]->find(s, idx + 1);
}
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> N;
Trie* root = new Trie();
for (int i = 0; i < N; i++) {
cin >> a[i];
int n = a[i];
string s;
for (int j = 0; j < 32; j++) {
if (n % 2)
s += '1';
else
s += '0';
n /= 2;
}
reverse(all(s));
root->insert(s, 0);
}
for (int i = 0; i < N; i++) {
int n = a[i];
string s;
for (int j = 0; j < 32; j++) {
if (n % 2)
s += '1';
else
s += '0';
n /= 2;
}
reverse(all(s));
root->find(s, 0);
int ret = 0;
for (int i = 0; i < 32; i++) {
if (s[i] == '1')
ret |= (1 << (31 - i));
}
ans = max(ans, ret);
}
cout << ans << "\n";
}