最大亦或对 字典树
给定一个1e5个数字,选出两个数A和B使得A xor B最大
先附上y总的讲题截图
推荐y总讲题链接
https://www.acwing.com/video/63/
很明显
假如 A=000100010011101
则 B一定要使得 B xor A前面部分的 1尽可能多
例如 B=010100010011101和B=100100010011101我们选后者
于是尽可能找前面位为 1的一定更优
- 对所有数字的二进制串建立字典树
- 对每个数字在字典树上找前面位为 1的值(即走相反的节点一定比走相同节点更优)
#define ch ( x >> i & 1 )
#define N 31
#if 0
void insert(int x) {
Node* now = root;
for(int i=N; i>=0; i--) {
if(!now->next[ch]) now->next[ch] = new Node();
now = now->next[ch];
}
}
int search(int x) {
Node* now = root;
int ans = 0;
for(int i=N; i>=0; i--) {
if(now->next[!ch]) {
ans += 1 << i;
now = now->next[!ch];
} else {
now = now->next[ch];
}
}
return ans;
}
#endif
int a[MAXN], tot;
int tree[MAXN*32][2];
void insert(int x) {
int now = 0;
for(int i=N; i>=0; i--) {
if(!tree[now][ch]) tree[now][ch] = ++tot;
now = tree[now][ch];
}
}
int search(int x) { //找打一个数,尽量使得二进制位前面的1最多
int now = 0, ans = 0;
for(int i=N; i>=0; i--) {
if(tree[now][!ch]) { //因为不同才为1,所以每一步尽量走相反的位
ans += 1 << i;
now = tree[now][!ch];
} else {
now = tree[now][ch];
}
}
return ans;
}
//main函数里
for(int i=1; i<=n; i++)
ans = max(ans, search(a[i]));
完整代码
#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)
using namespace std;
#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
namespace FastIO{
char print_f[105];
void read() {}
void print() { putchar('\n'); }
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
x = 0;
char ch = getchar();
ll f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
ll p3=-1;
if(x<0) putchar('-'), x=-x;
do{
print_f[++p3] = x%10 + 48;
} while(x/=10);
while(p3>=0) putchar(print_f[p3--]);
putchar(' ');
print(oth...);
}
} // namespace FastIO
using FastIO::print;
using FastIO::read;
int n, m, Q, K;
struct Node {
Node* next[2];
Node() { memset(next, false, sizeof(next)); }
} *root = new Node();
#define ch ( x >> i & 1 )
#define N 31
#if 0
void insert(int x) {
Node* now = root;
for(int i=N; i>=0; i--) {
if(!now->next[ch]) now->next[ch] = new Node();
now = now->next[ch];
}
}
int search(int x) {
Node* now = root;
int ans = 0;
for(int i=N; i>=0; i--) {
if(now->next[!ch]) {
ans += 1 << i;
now = now->next[!ch];
} else {
now = now->next[ch];
}
}
return ans;
}
#endif
int a[MAXN], tot;
int tree[MAXN*32][2];
void insert(int x) {
int now = 0;
for(int i=N; i>=0; i--) {
if(!tree[now][ch]) tree[now][ch] = ++tot;
now = tree[now][ch];
}
}
int search(int x) { //找打一个数,尽量使得二进制位前面的1最多
int now = 0, ans = 0;
for(int i=N; i>=0; i--) {
if(tree[now][!ch]) { //因为不同才为1,所以每一步尽量走相反的位
ans += 1 << i;
now = tree[now][!ch];
} else {
now = tree[now][ch];
}
}
return ans;
}
int main() {
#ifdef debug
freopen("test", "r", stdin);
clock_t stime = clock();
#endif
read(n);
for(int i=1, x; i<=n; i++) {
scanf("%d ", &x);
insert(x);
a[i] = x;
}
int ans = 0;
for(int i=1; i<=n; i++)
ans = max(ans, search(a[i]));
printf("%d\n", ans);
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}