XOR maximum of
Link to the original question: the largest of the XOR
Subject to the effect
You \ (n \) number, XORed selected from two numbers to get the maximum result is the number
Topic solution to a problem
A never wrote do not know how to write routines title
At first analysis can be greedy, that is to say if the two numbers can be as large as possible so high we would like as large as possible, that is to say two binary numbers at the same bit different, this time the largest, so how soon meet it? We found that can be converted into a binary number Trie above resolve the current 1 0 to go as far as possible, the current is 0 to 1 to go as far as possible, that this problem is very simple, to build a number of direct Trie ran just fine
//#define fre yes
#include <cstdio>
#include <iostream>
const int N = 100005;
struct Node {
int son[2];
} trie[N * 30];
int a[N], idx;
void Insert(int x) {
int rt = 0;
for (int i = 30; i >= 0; i--) {
int id = x >> i & 1;
if(!trie[rt].son[id]) trie[rt].son[id] = ++idx;
rt = trie[rt].son[id];
}
}
int Search(int x) {
int rt = 0, ans = 0;
for (int i = 30; i >= 0; i--) {
int id = x >> i & 1;
if(trie[rt].son[!id]) {
rt = trie[rt].son[!id];
ans += (1 << i);
} else rt = trie[rt].son[id];
} return ans;
}
int main() {
static int n, ans;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
Insert(a[i]);
} for (int i = 1; i <= n; i++) ans = std::max(ans , Search(a[i]));
printf("%d\n", ans);
return 0;
}