[HDU] 4825 (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


Source

2014年百度之星程序设计大赛 - 资格赛

Solution

本蒟蒻的第一道 01字典树的题目,算是了解了这个数据结构.

1) 插入
操作如平常的 Tire 树,每次都通过这个树的二进制下位来确定位置.
同时另开一个数组表示节点.再最后打一个标记大小.

2) 查询
每一次只要找和自己这一位不同的即可,如果有,那么就继续沿着走,否则就走相同的. 如果已经没有路可走了,那么直接返回值.

然后对着别人板子打了一波,代码如下.

代码

//HDU 4825
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
typedef long long ll;
ll c[maxn],n,m;
int ch[32 * maxn][2];
ll value[32 * maxn];
int node_cnt;                       

inline void init()  //将整个数组清零
{
    node_cnt = 1;
    memset(ch[0],0,sizeof(ch));
}           

inline void insert(ll x){           

    int cur = 0;
    for(int i = 32;i >= 0;--i){
        int idx = (x >> i) & 1;
        if(!ch[cur][idx]){
            memset(ch[node_cnt],0,sizeof(ch[node_cnt]));
            ch[cur][idx] = node_cnt;
            value[node_cnt++] = 0;
        }
            cur = ch[cur][idx];
        }
        value[cur] = x;
}

inline ll query(ll x){
    int cur = 0;
    for(int i = 32;i >= 0;--i){
        int idx = (x >> i) & 1;
        if(ch[cur][idx ^ 1]) cur = ch[cur][idx ^ 1];
        else cur = ch[cur][idx];
    }
    return value[cur];
}

int main()
{
    int t; scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        init();
        scanf("%lld%lld",&n,&m);
        for(int j=1;j<=n;j++)
        {scanf("%lld",&c[j]); insert(c[j]);}
        cout<<"Case"<<' '<<'#'<<i<<':'<<endl;
        while(m--)
        {
            int x; scanf("%d",&x);
            cout<<query(x)<<endl;
        }
    }
} 

原文地址:https://www.cnblogs.com/Kv-Stalin/p/9215650.html

时间: 2024-08-27 14:38:42

[HDU] 4825 (01字典树)的相关文章

Xor Sum HDU - 4825(01字典树)

Xor Sum HDU - 4825 (orz从之前从来没见过这种题 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 const int maxnode = 100010 * 32; 5 const int sigma = 2; 6 struct AC01{ 7 int ch[maxnode][sigma]; 8 LL val[maxnode]; 9 int sz; 10 void init(

HDU 4825 Xor Sum(01字典树入门题)

http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的数建立01字典树,从高位开始建树.对于每个询问,如果当前位置值为0,那么在字典树中,如果有1的值,那么就优先走1,否则再走0. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using names

HDU - 4825 Xor Sum(01字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题目: Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大.Prometheus 为了让 Zeus 看到人类的伟

HDU 4825 Xor Sum 【01字典树】

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

01字典树(待更新)

01字典树典型的题就是找出异或值最大的两个数,其实跟字典树差不多的,就是从原来的26位字母变成了0和1,插入操作也跟字典树差不多,查询的时候有贪心思想,尽量找同位不相同的. 模板: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 const int N=1e5+5; 8 9 struct n

Chip Factory---hdu5536(异或值最大,01字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:有一个数组a[], 包含n个数,从n个数中找到三个数使得 (a[i]+a[j])⊕a[k]最大,i,j,k不同; 求异或的结果最大所以我们可以用01字典树,先把所有的数加入字典树中,从n个数中选出两个数a[i]和a[j], 先把他们从字典树中删除,然后找到与a[i]+a[j]异或最大的数,和结果取最大值即可: 最后不要忘记再把a[i]和a[j]添加到字典树中即可: #include<st

Chip Factory(HDU5536 + 暴力 || 01字典树)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目: 题意: 对于给定的n个数,求出三个下标不同的数使得(si+sj)^sk最大. 思路: 由于时间给了9s,所以可以暴力过.不过还可以用01字典树艹过去,不过注意字典树里面存si查询(sj+sk),不要存(si+sj)查询sk,不然会T. 暴力代码实现如下: 1 #include <set> 2 #include <map> 3 #include <deque>

HDU6191(01字典树启发式合并)

Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 801    Accepted Submission(s): 302 Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey

2014百度之星资格赛—— Xor Sum(01字典树)

Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起