标签:集合 xor sum 个数 结构体 include 测试数据 xor put name
1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 const int N = 100010; 6 7 struct Nod { 8 int id; 9 Nod *next[2]; 10 Nod() { 11 next[0] = next[1] = NULL; 12 id = 0; 13 } 14 }; 15 int t, n, m, x, cas; 16 void mkTree(Nod *root,int x) { 17 Nod *p = root; 18 for(int i = 31; i >= 0; i --) { 19 int a = (x>>i)&1; 20 if(p->next[a] == NULL) p->next[a] = new Nod; 21 p = p->next[a]; 22 } 23 p->id = x; 24 } 25 int find(Nod *root,int x) { 26 Nod *p = root; 27 for(int i = 31; i >= 0; i --) { 28 int a = (x>>i)&1; 29 if(p->next[a^1] != NULL) p = p->next[a^1]; 30 else p = p->next[a]; 31 } 32 return p->id; 33 } 34 void dele(Nod *p) { 35 for(int i = 0; i < 2; i ++) { 36 if(p->next[i]) 37 dele(p->next[i]); 38 } 39 free(p); 40 return; 41 } 42 int main() { 43 scanf("%d", &t); 44 while(t--) { 45 Nod *root = new Nod; 46 scanf("%d %d", &n, &m); 47 for(int i = 1; i <= n; i ++) { 48 scanf("%d", &x); 49 mkTree(root,x); 50 } 51 printf("Case #%d:\n",++cas); 52 while(m--) { 53 scanf("%d", &x); 54 printf("%d\n",find(root,x)); 55 } 56 dele(root); 57 } 58 return 0; 59 }
数组:
1 #include <stdio.h> 2 #include <string.h> 3 using namespace std; 4 const int N = 3500005; 5 int sz, tree[N], sum[N][2]; 6 7 void mkTree(int x) { 8 int root = 0; 9 for(int i = 31; i >= 0; i --) { 10 int a = (x>>i)&1; 11 if(!sum[root][a]) sum[root][a] = sz++; 12 root = sum[root][a]; 13 } 14 tree[root] = x; 15 } 16 int find(int x) { 17 int root = 0; 18 for(int i = 31; i >= 0; i --) { 19 int a = (x>>i)&1; 20 if(sum[root][a^1]) root = sum[root][a^1]; 21 else root = sum[root][a]; 22 } 23 return tree[root]; 24 } 25 int main() { 26 int t, n, m, x, cas = 1; 27 scanf("%d", &t); 28 while(t--) { 29 sz = 1; 30 memset(sum, 0, sizeof(sum)); 31 memset(tree, 0, sizeof(tree)); 32 scanf("%d %d", &n, &m); 33 while(n--) { 34 scanf("%d", &x); 35 mkTree(x); 36 } 37 printf("Case #%d:\n", cas++); 38 while(m--) { 39 scanf("%d", &x); 40 printf("%d\n",find(x)); 41 } 42 } 43 return 0; 44 }
标签:集合 xor sum 个数 结构体 include 测试数据 xor put name
原文地址:http://www.cnblogs.com/xingkongyihao/p/7295501.html