hdu4825 Xor Sum(01字典树)
题意:给你一个整数数列,然后在给你一个数,让你在这个整数数列中寻找到一个数使得与给定数字的异或最大。思路:把整数序列建立起一棵01字典树,然后查找即可。(还是开 ll 比较好)。AC Code:#include<iostream>#include<cstring>#include<queue>#include<map>#incl...
·
题意:给你一个整数数列,然后在给你一个数,让你在这个整数数列中寻找到一个数使得与给定数字的异或最大。
思路:把整数序列建立起一棵01字典树,然后查找即可。(还是开 ll 比较好)。
AC Code:
#include<iostream>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<cstdio>
#include<iomanip>
#include<sstream>
#include<algorithm>
using namespace std;
#define read(x) scanf("%d",&x)
#define Read(x,y) scanf("%d%d",&x,&y)
#define sRead(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define gc(x) scanf(" %c",&x);
#define mmt(x,y) memset(x,y,sizeof x)
#define write(x) printf("%d\n",x)
#define ios ios::sync_with_stdio(false);cin.tie(0);
#define INF 0x3f3f3f3f
#define ll long long
const ll mod = 1e9 + 7;
#define pii pair<int,int>
#define pdd pair<double,double>
const int N = 4e5+5;
const int M = 1e7 + 5;
int trie[M][2],tot = 1;
void Insert(ll u){
ll p = 1;
for(ll i = 31;i >= 0; -- i){
int ch = (u >> i)&1;
if(trie[p][ch] == 0) trie[p][ch] = ++tot;
p = trie[p][ch];
}
}
ll Search(ll u){
ll p = 1,ans = 0;
for(int i = 31;i >= 0;i --){
int ch = (u >> i)&1;
if(trie[p][!ch]) {
if(ch == 0){
ans += (1LL << i);
}
p = trie[p][!ch];
}
else {
p = trie[p][ch];
if(ch == 1) ans += (1 << i);
}
}
return ans;
}
inline void init(){
mmt(trie,0);
tot = 1;
}
int main()
{
//freopen("input.txt","r",stdin);
int T;
read(T);
int n,m;
for(int p = 1;p <= T;++p){
init();
Read(n,m);
ll u;
for(int i = 1;i <= n;++i){
scanf("%lld",&u);
Insert(u);
}
printf("Case #%d:\n",p);
for(int i =1;i <= m;++i){
scanf("%lld",&u);
printf("%lld\n",Search(u));
}
}
}
更多推荐
所有评论(0)