-
Notifications
You must be signed in to change notification settings - Fork 0
/
10050.cpp
75 lines (72 loc) · 1.77 KB
/
10050.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>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int maxn = 100000 + 5;
int ch[maxn * 30][2], val[maxn * 30], tot;
void insert(char* s) {
int now = 0;
for (int i = 0; s[i]; i++) {
if (ch[now][s[i] - '0']) now = ch[now][s[i] - '0'];
else now = ch[now][s[i] - '0'] = ++tot;
}
val[now]++;
}
void insert(int x) {
char tmp[35];
for (int i = 30; i >= 0; i--) {
tmp[30 - i] = ((x >> i) & 1) + '0';
}
tmp[31] = 0;
// cout << tmp << endl;
insert(tmp);
}
int get(char* s) {
int x = 0;
for (int i = 0; i <= 30; i++) {
if (s[i] == '1') x += (1 << (30 - i));
}
// cout << x << endl;
return x;
}
int query(int x) {
char tmp[35];
for (int i = 30; i >= 0; i--) {
tmp[30 - i] = ((x >> i) & 1) + '0';
}
tmp[31] = 0;
int now = 0, ans = 0;
for (int i = 0; tmp[i]; i++) {
if (tmp[i] == '1') {
if (ch[now][0]) now = ch[now][0];
else if (ch[now][1]) tmp[i] = '0', now = ch[now][1];
else return ans;
} else {
if (ch[now][1]) tmp[i] = '1', now = ch[now][1];
else if (ch[now][0]) now = ch[now][0];
else return ans;
}
if (val[now]) ans = max(ans, get(tmp));
}
return ans;
}
int n, a[maxn];
int main(){
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
insert(a[i]);
}
int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, query(a[i]));
}
cout << ans << endl;
return 0;
}