题目链接:https://vjudge.net/problem/HDU-4825

Xor Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 3839    Accepted Submission(s): 1685

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
 
Recommend
liuyiding

题解:

将所有数按二进制从高位到低位插入到Trie树中,然后再枚举每一个数,去Trie中反向匹配即可。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; struct Trie
{
int next[*MAXN][];
int root, L = ;
int newnode()
{
next[L][] = next[L][] = -;
L++;
return L-;
}
void init()
{
L = ;
root = newnode();
}
void insert(LL val)
{
int tmp = root, way;
for(int i = ; i>=; i--)
{
way = (val>>i)&;
if(next[tmp][way]==-) next[tmp][way] = newnode();
tmp = next[tmp][way];
}
}
LL query(LL val)
{
LL ret = ;
int tmp = root, way;
for(int i = ; i>=; i--)
{
ret <<= ;
way = (val>>i)&;
if(next[tmp][!way]!=-) //如果另一方向存在,则走另一方向
ret ^= !way, tmp = next[tmp][!way];
else //否则,顺着走
ret ^= way, tmp = next[tmp][way];
}
return ret;
}
};
Trie T; int main()
{
int n, m, TT;
scanf("%d",&TT);
for(int kase = ; kase<=TT; kase++)
{
scanf("%d%d",&n,&m);
T.init();
for(int i = ; i<=n; i++)
{
LL val;
scanf("%lld",&val);
T.insert(val);
} printf("Case #%d:\n",kase);
while(m--)
{
LL val;
scanf("%lld",&val);
printf("%lld\n", T.query(val));
}
}
}

HDU4825 Xor Sum —— Trie树的更多相关文章

  1. HDU--4825 Xor Sum (字典树)

    题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...

  2. hdu 4825 Xor Sum trie树

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Proble ...

  3. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

  4. HDU4825 Xor Sum(贪心+Trie树)

    Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...

  5. HDU4825 Xor Sum(字典树解决最大异或问题)

    Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整 ...

  6. HDU-4825 Xor Sum,字典树好题!

    Xor Sum 一遍A了之后大呼一声好(keng)题!debug了两小时~~~~百度之星资格赛,可以. 题意:给你一个n个元素的数组,m次查询,每次输入一个数k要求从数组中找到一个数与k异或值最大,输 ...

  7. usaco6.1-Cow XOR:trie树

    Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...

  8. HDU 4825 Xor Sum 字典树+位运算

    点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

  9. 2014百度之星第三题Xor Sum(字典树+异或运算)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

随机推荐

  1. 【音乐App】—— Vue-music 项目学习笔记:用户个人中心开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 歌曲列表 收藏歌曲 一.用 ...

  2. GlusterFS源代码解析 —— GlusterFS 内存分配方式

    原文地址:http://blog.csdn.net/wangyuling1234567890/article/details/24564891 GlusterFS 的内存分配主要有两种方式,一种是内存 ...

  3. 【VBS】检索Outlook本地邮箱

    实现功能:使用VBS检索Outlook本地邮箱中,今天是否收到某标题的邮件. 代码如下: ' yyyy-m-d 0:00 AM ' yyyy-m-d 11:59 PM Function CheckMa ...

  4. binary-tree-inorder-traversal——二叉树中序遍历

    Given a binary tree, return the inordertraversal of its nodes' values. For example:Given binary tree ...

  5. Shell脚本之:变量

    与编译型语言不同,shell脚本是一种解释型语言. 执行这类程序时,解释器(interpreter)需要读取我们编写的源代码(source code),并将其转换成目标代码(object code), ...

  6. 怎样在编译的时候,控制删除apk不用的资源?

    1.改动alps/meidatek/config/xxx/ProjectConfig.mk下的MTK_PRODUCT_LOCALS,去掉不用的资源,比方屏幕密度或语言等.  2. nodpi和mipm ...

  7. 高阶函数:map()/reduce()

    Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Large Clus ...

  8. python之prettytable

    sdata={'语文':89,'数学':96,'音乐':39,'英语':78,'化学':88} #字典向Series转化 >>> studata=Series(sdata) > ...

  9. oracle不同用户间访问表不添加用户名(模式)前缀

    默认的情况下,oracle里面的用户A,要访问用户B的表需要带用户B的前缀,如访问用户B的 user表,需要这样访问 select * from B.user;如果想要不添加用户前缀,需要这样处理: ...

  10. 用HttpClient模拟HTTP的GET和POST请求(转)

    本文转自:http://blog.csdn.net/xiazdong/article/details/7724349 一.HttpClient介绍   HttpClient是用来模拟HTTP请求的,其 ...