#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#define INF 0x3f3f3f3f3f3f3f
using namespace std;
const int MAXN = 2e5 + 10;
int node;
int next[MAXN][2];
int ed[MAXN];
void add(int cur, int k)
{
memset(next[node], 0, sizeof(next[node]));
next[cur][k] = node++;
//cout << "~~" << node << " ";
}
int cal(int x)
{
int cur = 0;
for (int i = 30; i >= 0; --i) {
int k = (x >> i) & 1;
if (next[cur][1-k]) cur = next[cur][1-k];
else cur = next[cur][k];
}
//cout << x << " " << ed[cur] << endl;
return (x ^ ed[cur]);
}
int main()
{
int n;
cin >> n;
int a[MAXN];
node = 1;
next[0][0] = next[0][1] = 0;
memset(ed, 0, sizeof(ed));
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
int cur = 0;
for (int j = 30; j >= 0; --j) {
int k = (a[i] >> j) & 1;
if (next[cur][k] == 0) add(cur, k);
cur = next[cur][k];
}
ed[cur] = a[i];
}
int ans = 0;
for (int i = 0; i < n; ++i)
ans = max(ans, cal(a[i]));
cout << ans << endl;
int ans2 = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
ans2 = max(ans2, a[i] ^ a[j]);
}
}
cout << ans2 << endl;
return 0;
}
求n个数中两数异或的最大值(字母树)
最新推荐文章于 2021-10-27 13:52:00 发布