Zeus 和 Prometheus 做了一個遊戲,Prometheus 給 Zeus 一個集合,集合中包含了N個正整數,隨後 Prometheus 將向 Zeus 發起M次詢問,每次詢問中包含一個正整數 S ,之後 Zeus 需要在集合當中找出一個正整數 K ,使得 K 與 S 的異或結果最大。Prometheus 爲了讓 Zeus 看到人類的偉大,隨即同意 Zeus 可以向人類求助。你能證明人類的智慧麼?
Input
輸入包含若干組測試數據,每組測試數據包含若干行。
輸入的第一行是一個整數T(T < 10),表示共有T組數據。
每組數據的第一行輸入兩個正整數N,M(<1=N,M<=100000),接下來一行,包含N個正整數,代表 Zeus 的獲得的集合,之後M行,每行一個正整數S,代表 Prometheus 詢問的正整數。所有正整數均不超過2^32。
Output
對於每組數據,首先需要輸出單獨一行”Case #?:”,其中問號處應填入當前的數據組數,組數從1開始計算。
對於每個詢問,輸出一個正整數K,使得K與S異或值最大。
Sample Input
2
3 2
3 4 5
1
5
4 1
4 6 5 6
3
Sample Output
Case #1:
4
3
Case #2:
4
#include<bits/stdc++.h>
using namespace std;
#define maxn 100010
typedef long long ll;
struct Tire{
int siz,ch[32*maxn][2],val[32*maxn];
void tire_clear(){siz=1,memset(ch[0],0,sizeof(ch[0]));}
void tire_insert(int x){
int u=0;
for(int i=31;i>=0;i--){
int c=((1<<i)&x)?1:0;
if(!ch[u][c]){//節點不存在
memset(ch[siz],0,sizeof(ch[siz]));
val[siz]=0;
ch[u][c]=siz++;//新建節點
}
u=ch[u][c];//往下走
}
val[u]=x;//字符串的最後一個字符的附加信息爲v
}
int tire_find(int x){
int u=0;
for(int i=31;i>=0;i--){
int c=((1<<i)&x)?1:0;
if(c==0) u=ch[u][1]?ch[u][1]:ch[u][0];
else u=ch[u][0]?ch[u][0]:ch[u][1];
}
return val[u];
}
}tire;
int main(){
int t,cas=0; scanf("%d",&t);
while(t--){
printf("Case #%d:\n",++cas);
tire.tire_clear();
int n,m; scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
int x; scanf("%d",&x);
tire.tire_insert(x);
}
for(int i=1;i<=m;i++){
int x; scanf("%d",&x);
printf("%d\n",tire.tire_find(x));
}
}
return 0;
}