本文主要是介绍hdu 4825,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//我自创的字典树哦:)#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <fstream>
using namespace std;
const int N=100010;
const long long bit=2147483648;
struct node{int l,r;node(): l(0), r(0) {}
}tre[40*N];
int num=0;void add(long long x)
{long long a[32];memset(a,0,sizeof(a));int tmp,i=31;while( x ){a[i--]=x%2;x/=2;}tmp=0;for(i=0;i<32;i++){if( a[i]==0 && !tre[tmp].l ){tre[tmp].l=++num;tmp=num;}else if( a[i]==1 && !tre[tmp].r ){tre[tmp].r=++num;tmp=num;}else{tmp=(a[i]==0 ? tre[tmp].l:tre[tmp].r);}}
}long long getAns(long long x)
{long long a[32],ans,BIT;memset(a,0,sizeof(a));int tmp,i=31;while( x ){a[i--]=x%2;x/=2;}tmp=0; ans=0; BIT=bit;for(i=0;i<32;i++){if( a[i]==0 ){if( tre[tmp].r>0 ){ans+=BIT;tmp=tre[tmp].r;}else tmp=tre[tmp].l;}else{if( tre[tmp].l>0 ){tmp=tre[tmp].l;}else{tmp=tre[tmp].r;ans+=BIT;}}BIT/=2;}return ans;
}int main()
{
// freopen("in","r",stdin);
// freopen("out","w",stdout);int t,i,j,n,m;long long x,ans;scanf("%d",&t);for(i=1;i<=t;i++){scanf("%d%d",&n,&m);num=0;for(j=0;j<=32*n;j++)tre[j].l=tre[j].r=0;while( n-- ){scanf("%I64d",&x);add(x);}printf("Case #%d:\n",i);while( m-- ){scanf("%I64dd",&x);ans=getAns(x);printf("%I64d\n",ans);}}return 0;
}
这篇关于hdu 4825的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!