2 3 2 3 4 5 1 5 4 1 4 6 5 6 3
Case #1: 4 3 Case #2: 4
#include <iostream> #include <cstring> #include <sstream> #include <cstdio> #include <cmath> #include <bitset> #include <map> #include <queue> #include <vector> #include <cstdlib> #include <algorithm> #define ls u << 1 #define rs u << 1 | 1 #define lson l, mid, u << 1 #define rson mid + 1, r, u << 1 | 1 #define INF 0x3f3f3f3f #define MAX 2 using namespace std; typedef long long ll; typedef unsigned long long ull; const int M = 1e4 + 100; const int mod = 1e9 + 7; int a[40],maxn; struct Trie { int index; Trie *next[MAX]; Trie() { index = 0; memset(next,0,sizeof(next)); } }; void Trie_insert(Trie *tr,int len) { if(len >= 0) { int u = a[len]; if(tr->next[u] == 0) tr->next[u] = new Trie; Trie_insert(tr->next[u],len - 1); } } void dfs(Trie *tr,int len) { if(tr == NULL || len < 0) return; int u = a[len]; if(tr->next[!u]) { maxn = maxn * 2 + !u; dfs(tr->next[!u],len - 1); } else { maxn = maxn * 2 + u; dfs(tr->next[u],len - 1); } } int main() { int T,n,m,cnt = 0; scanf("%d",&T); while(T--) { Trie *root = new Trie; scanf("%d %d",&n,&m); for(int i = 0; i < n; i++) { int x; scanf("%d",&x); memset(a,0,sizeof(a)); for(int i = 0; x; i++) { a[i] = x & 1; x >>= 1; } Trie_insert(root,31); } printf("Case #%d:\n",++cnt); while(m--) { int x; maxn = 0; scanf("%d",&x); memset(a,0,sizeof(a)); for(int i = 0; x; i++) { a[i] = x & 1; x >>= 1; } dfs(root,31); printf("%d\n",maxn); } } return 0; }
原文地址:http://blog.csdn.net/zsgg_acm/article/details/44100881