HDU-6625 题目链接
很简单的思路,直接就是去开两个字典树,然后两个字典树一块跑就可以了,每次都是贪心的选取最小异或值即可。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MAX_3(x, y, z) max(x, max(y, z))
#define MAX_6(a, b, c, d, f, g) max(max(a, b), max(max(c, d), max(f, g)))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, trie[2][maxN*31][2], cnt[2][maxN*31], tot[2];
inline void clear(int op, int rt)
{
for(int i=0; i<=1; i++) trie[op][rt][i] = 0;
cnt[op][rt] = 0;
}
inline void Insert(int op, int x)
{
int root = 0, bit = 0;
for(int i=30; i>=0; i--)
{
bit = (x >> i) & 1;
if(!trie[op][root][bit])
{
trie[op][root][bit] = ++tot[op];
clear(op, tot[op]);
}
root = trie[op][root][bit];
cnt[op][root]++;
}
}
inline void Delete(int op, int x)
{
int root = 0, bit = 0;
for(int i=30; i>=0; i--)
{
bit = (x >> i) & 1;
if(!( --cnt[op][trie[op][root][bit]] ))
{
trie[op][root][bit] = 0;
break;
}
root = trie[op][root][bit];
}
}
int ans[maxN];
inline void find_first(int &x0, int &x1)
{
int rt0 = 0, rt1 = 0;
x0 = 0; x1 = 0;
for(int i=30; i>=0; i--)
{
if(trie[0][rt0][0] && trie[1][rt1][0])
{
rt0 = trie[0][rt0][0];
rt1 = trie[1][rt1][0];
}
else if(trie[0][rt0][1] && trie[1][rt1][1])
{
rt0 = trie[0][rt0][1];
rt1 = trie[1][rt1][1];
x0 |= 1 << i;
x1 |= 1 << i;
}
else
{
if(trie[0][rt0][0])
{
rt0 = trie[0][rt0][0];
rt1 = trie[1][rt1][1];
x1 |= 1 << i;
}
else
{
rt0 = trie[0][rt0][1];
rt1 = trie[1][rt1][0];
x0 |= 1 << i;
}
}
}
}
inline void init()
{
tot[0] = tot[1] = 0;
for(int i=0; i<=1; i++)
{
cnt[i][0] = 0;
for(int j=0; j<=1; j++) trie[i][0][j] = 0;
}
}
int main()
{
int Cas; scanf("%d", &Cas);
while(Cas--)
{
scanf("%d", &N);
init();
for(int i=1, val; i<=N; i++)
{
scanf("%d", &val);
Insert(0, val);
}
for(int i=1, val; i<=N; i++)
{
scanf("%d", &val);
Insert(1, val);
}
for(int i=1, x0, x1; i<=N; i++)
{
find_first(x0, x1);
ans[i] = x0 ^ x1;
Delete(0, x0); Delete(1, x1);
}
sort(ans + 1, ans + N + 1);
for(int i=1; i<=N; i++) printf("%d%c", ans[i], i == N ? '\n' : ' ');
}
return 0;
}