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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值