标签:集合 正整数 == xor 字典树 12px ssi 释放 mission
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 2403 Accepted Submission(s): 1041
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 5 struct node 6 { 7 node *child[2]; 8 node(){child[0]=child[1]=NULL;} 9 }; 10 node *root; 11 void Clear(node *p) 12 { 13 if(p==NULL) return; 14 for(int i=0;i<2;++i){ 15 if(p->child[i]!=NULL) Clear(p->child[i]); 16 } 17 delete p; 18 } 19 void add(LL num) 20 { 21 node *p=root; 22 int i,bit; 23 for(i=32;i>=0;--i){ 24 bit=(num&((LL)1<<i))?1:0; 25 if(p->child[bit]==NULL) 26 p->child[bit]=new node(); 27 p=p->child[bit]; 28 } 29 } 30 LL solve(LL num) 31 { 32 node *p=root; 33 int i,bit[2]; LL ans=0; 34 for(i=32;i>=0;--i){ 35 bit[0]=(num&((LL)1<<i))?1:0; 36 if(p->child[bit[0]^1]!=NULL){ 37 p=p->child[bit[0]^1]; 38 bit[1]=1; 39 } 40 else {p=p->child[bit[0]];bit[1]=0;} 41 if(bit[1]) ans+=((LL)1<<i); 42 } 43 return ans; 44 } 45 int main() 46 { 47 int T,N,M,i,j,k=0; 48 LL n; 49 cin>>T; 50 while(T--){root=new node(); 51 cin>>N>>M; 52 for(i=1;i<=N;++i){ 53 scanf("%lld",&n); 54 add(n); 55 }printf("Case #%d:\n",++k); 56 for(i=1;i<=M;++i){ 57 scanf("%lld",&n); 58 printf("%lld\n",n^solve(n)); 59 } 60 Clear(root); 61 } 62 return 0; 63 }
标签:集合 正整数 == xor 字典树 12px ssi 释放 mission
原文地址:http://www.cnblogs.com/zzqc/p/7219905.html