The meaning of problems:
Given two arrays a length of n and B;
rearranging a and b, to generate an array c, c [i] = a [i] xor b [i];
output array lexicographically smallest c.
Analysis:
The number of a trie inserting a 01 a; and
the number of b is inserted into a Trie b, 01;
Find trie tree n times, each time simultaneously in a one down and b ;
IF can simultaneously go to 0, while walking 0;
the else IF can go simultaneously 1, while walking 1;
the else IF trees a ambulatory 0 && tree b can go to 1, a walk 0, b walking 1;
the else IF trees a 1 && b can walk can walk the tree 0, then take a 1, b 0 down;
the else c to insert a new number for the exclusive-oR value of these two nodes;
final sort c.
Complexity of O (T * n * 30)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; const int maxn = 1e6+5; typedef long long LL; struct Trie01{ int ch[35 * maxn][2]; int cnt[35*maxn]; int node_cnt; inline void init(){ node_cnt = 1; memset(ch[0],0,sizeof(ch[0])); } inline void Insert(LL x){ int cur = 0; for(int i = 30;i >= 0;--i){ int idx = (x >> i) & 1; if(!ch[cur][idx]){ memset(ch[node_cnt],0,sizeof(ch[node_cnt])); ch[cur][idx] = node_cnt; cnt[node_cnt++]=0; } cur = ch[cur][idx]; cnt[cur]++; } } }t1,t2; vector<int> ans; void solve(int N){ int u1,u2; while(N--) { u1 = u2 = 0; int x = 0; for(int p = 30; p >= 0; p--) { if(t1.cnt[t1.ch[u1][0]] && t2.cnt[t2.ch[u2][0]]) { t1.cnt[t1.ch[u1][0]]--; t2.cnt[t2.ch[u2][0]]--; u1 = t1.ch[u1][0]; u2 = t2.ch[u2][0]; } else if(t1.cnt[t1.ch[u1][1]] && t2.cnt[t2.ch[u2][1]]) { t1.cnt[t1.ch[u1][1]]--; t2.cnt[t2.ch[u2][1]]--; u1 = t1.ch[u1][1]; u2 = t2.ch[u2][1]; } else if(t1.cnt[t1.ch[u1][0]] && t2.cnt[t2.ch[u2][1]]) { t1.cnt[t1.ch[u1][0]]--; t2.cnt[t2.ch[u2][1]]--; u1 = t1.ch[u1][0]; u2 = t2.ch[u2][1]; x ^= (1 << p); } else { t1.cnt[t1.ch[u1][1]]--; t2.cnt[t2.ch[u2][0]]--; u1 = t1.ch[u1][1]; u2 = t2.ch[u2][0]; x ^= (1 << p); } } ans.push_back(x); } return ; } int main(){ int _;scanf("%d",&_); while(_--){ int n;scanf("%d",&n); t1.init(),t2.init(); for(int i=1;i<=n;i++){ int x;scanf("%d",&x); t1.Insert(x); } for(int i=1;i<=n;i++){ int x;scanf("%d",&x); t2.Insert(x); } ans.clear(); solve(n); sort(ans.begin(),ans.end()); printf("%d",ans[0]); for(int i=1;i<ans.size();i++) { printf(" %d",ans[i]); }puts(""); } }