HDU 4825 Xor Sum (01字典树)

Problem Description
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大。Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助。你能证明人类的智慧么?

Input
输入包含若干组测试数据,每组测试数据包含若干行。
输入的第一行是一个整数T(T < 10),表示共有T组数据。
每组数据的第一行输入两个正整数N,M(<1=N,M<=100000),接下来一行,包含N个正整数,代表 Zeus 的获得的集合,之后M行,每行一个正整数S,代表 Prometheus 询问的正整数。所有正整数均不超过2^32。

Output
对于每组数据,首先需要输出单独一行”Case #?:”,其中问号处应填入当前的数据组数,组数从1开始计算。
对于每个询问,输出一个正整数K,使得K与S异或值最大。

Sample Input
2
3 2
3 4 5
1
5
4 1
4 6 5 6
3

Sample Output
Case #1:
4
3
Case #2:
4

本来想复习一下字典树,结果发现还有01字典树这种东西。找了一题来做。
题意,给你一些数字,再给你一些询问。每次询问给你一个数字,你根据这个数字再最初给你的那些数中找一个两个亦或起来值最大的,输出那个数。
做法很水,首先利用最初的那些树按二进制从高位到低位建一颗字典树。每次查询时候遇到一个节点,和当前数的当前位比较,尽量走亦或值大的即可。

#include "cstring"
#include "cstdio"
#include "cstring"
#include "string.h"
#include "cmath"
#include "algorithm"
#include "iostream"
using namespace std;
typedef struct node
{
    node *next[2];
    node()
    {
        memset(next,NULL,sizeof(next));
    }
}node;
node *root=new node();
void ins(long long num)
{
    node *now=root;
    for(int i=31;i>=0;i--)
    {
        if(((num>>i)&1)==0)
        {
            if(now->next[0]==NULL)
                now->next[0]=new node();
            now=now->next[0];
        }
        else
        {
            if(now->next[1]==NULL)
            now->next[1]=new node();
            now=now->next[1];
        }
    }
}
long long find(long long num)
{
    node *now=root;
    long long ans=0;
    for(int i=31;i>=0;i--)
    {
        if(((num>>i)&1)==0)
        {
            if(now->next[1]!=NULL)
            {
                now=now->next[1];
                ans+=(1<<i);
                continue;
            }
            else
            {
                if(now->next[0]!=NULL)
                {
                    now=now->next[0];
                    continue;
                }
            }
        }
        else
        {
            if(now->next[0]!=NULL)
            {
                now=now->next[0];
                continue;
            }
            else
            {
                if(now->next[1]!=NULL)
                {
                    now=now->next[1];
                    ans+=(1<<i);
                    continue;
                }
            }

        }
        break;
    }
    return ans;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    int t=1;
    while(cas--)
    {
        root=new node();
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            long long temp;
            scanf("%lld",&temp);
            ins(temp);
        }
        printf("Case #%d:\n",t++);
        for(int i=1;i<=m;i++)
        {
            long long temp;
            scanf("%lld",&temp);
            printf("%lld\n",find(temp));
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值