先上代码:
Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 132768/132768 K
(Java/Others)
Total Submission(s): 7837 Accepted
Submission(s): 3350
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <bitset> 5 #define NOT(x) (x == 1 ? 0 : 1) 6 #define MAX 3200002 7 #define LL long long 8 using namespace std; 9 10 typedef bitset<32> bs; 11 int s[MAX][2],tot; 12 13 void reset(){ 14 memset(s[0],0,sizeof(s[0])); 15 tot=1; 16 } 17 18 void insert(bs a){ 19 int j=0; 20 for(int i=31;i>=0;i--){ 21 int u = a[i]; 22 if(!s[j][u]){ 23 memset(s[tot],0,sizeof(s[tot])); 24 s[j][u] = tot++; 25 } 26 j = s[j][u]; 27 } 28 } 29 30 LL check(bs a){ 31 LL r=0; 32 int j=0; 33 for(int i=31;i>=0;i--){ 34 int u = a[i]; 35 r<<=1; 36 if(s[j][NOT(u)]){ 37 r += NOT(u); 38 j = s[j][NOT(u)]; 39 }else if(s[j][u]){ 40 r +=u; 41 j = s[j][u]; 42 } 43 44 } 45 return r; 46 } 47 48 int main() 49 { 50 LL t,n,m,a; 51 LL r; 52 //freopen("data.txt","r",stdin); 53 scanf("%I64d",&t); 54 for(int z=1;z<=t;z++){ 55 printf("Case #%d:\n",z); 56 scanf("%I64d %I64d",&n,&m); 57 reset(); 58 for(int i=0;i<n;i++){ 59 scanf("%I64d",&a); 60 bs e(a); 61 insert(e); 62 } 63 for(int i=0;i<m;i++){ 64 scanf("%I64d",&a); 65 bs e(a); 66 r=check(e); 67 printf("%I64d\n",r); 68 } 69 } 70 return 0; 71 }
百度之星2014资格赛 1003 - Xor Sum,布布扣,bubuko.com
原文:http://www.cnblogs.com/sineatos/p/3737880.html