hdu4825 Xor Sum

找两个异或和最大的数
很容易想到trie树维护二进制

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
struct Node{
	ll son[2], idd;
	bool hav[2];
	Node(){
		son[0] = son[1] = idd = hav[1] = hav[0] = false;
	}
}trie[3500005];
int T, n, m, cnt;
ll tmp;
void ins(ll uu, ll id){
	ll u=0;
	for(int i=32; i>=0; i--){
		ll ss=(uu&(1ll<<i))>0;
		if(!trie[u].hav[ss]){
			trie[u].hav[ss] = true;
			trie[u].son[ss] = ++cnt;
		}
		u = trie[u].son[ss];
	}
	trie[u].idd = id;
}
ll sol(ll uu){
	ll re=0, u=0;
	for(int i=32; i>=0; i--){
		ll ss=(uu&(1ll<<i))>0;
		if(ss){
			if(trie[u].hav[0])	u = trie[u].son[0];
			else				u = trie[u].son[1];
		}
		else{
			if(trie[u].hav[1])	re |= 1<<i, u = trie[u].son[1];
			else				u = trie[u].son[0];
		}
	}
	return trie[u].idd;
}
int main(){
	cin>>T;
	for(int ii=1; ii<=T; ii++){
		printf("Case #%d:\n", ii);
		cnt = 0;
		memset(trie, 0, sizeof(trie));
		scanf("%d %d", &n, &m);
		for(int i=1; i<=n; i++){
			scanf("%lld", &tmp);
			ins(tmp, tmp);
		}
		while(m--){
			scanf("%lld", &tmp);
			printf("%lld\n", sol(tmp));
		}
	}
	return 0;
}
posted @ 2017-11-30 19:07  poorpool  阅读(95)  评论(0编辑  收藏  举报