HDU4825 Xor Sum —— Trie树
题目链接: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
输入的第一行是一个整数T(T < 10),表示共有T组数据。
每组数据的第一行输入两个正整数N,M(<1=N,M<=100000),接下来一行,包含N个正整数,代表 Zeus 的获得的集合,之后M行,每行一个正整数S,代表 Prometheus 询问的正整数。所有正整数均不超过2^32。
对于每个询问,输出一个正整数K,使得K与S异或值最大。
3 2
3 4 5
1
5
4 1
4 6 5 6
3
4
3
Case #2:
4
题解:
将所有数按二进制从高位到低位插入到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树的更多相关文章
- HDU--4825 Xor Sum (字典树)
题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...
- hdu 4825 Xor Sum trie树
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Proble ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- HDU4825 Xor Sum(贪心+Trie树)
Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...
- HDU4825 Xor Sum(字典树解决最大异或问题)
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整 ...
- HDU-4825 Xor Sum,字典树好题!
Xor Sum 一遍A了之后大呼一声好(keng)题!debug了两小时~~~~百度之星资格赛,可以. 题意:给你一个n个元素的数组,m次查询,每次输入一个数k要求从数组中找到一个数与k异或值最大,输 ...
- usaco6.1-Cow XOR:trie树
Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...
- HDU 4825 Xor Sum 字典树+位运算
点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- 2014百度之星第三题Xor Sum(字典树+异或运算)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
随机推荐
- project修改时间日历
视图→甘特图 格式→时间表→右键时间表 详细的日程表,然后双击时间即可
- FileUpload控件预览图片
HTML代码: <tr> <td class="auto-style1">上传图片:</td> <td> <asp:FileU ...
- C# Excel
using System.IO;using System.Text;namespace iLIS.Common{ ///<summary> ///生成Excel文档内容 /// 存入工作流 ...
- \\s+ split替换
出自: http://www.tuicool.com/articles/vy2ymm 详解 "\\s+" 正则表达式中\s匹配任何空白字符,包括空格.制表符.换页符等等, 等价于[ ...
- StringBuilder作用
String 类型和 StringBuffer 类型的主要性能区别其实在于 String 是不可变的对象因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然 ...
- Hadoop--设置单一节点集群
目的 这篇文档描述如何安装和配置一个单一节点的Hadoop,以便你可以快速使用hadoop mapreduce和Hadoop Distributed File System (HDFS)的一些简单操作 ...
- FTP 连接报错
Filezilla 站点管理器=>选中FTP站点=>加密(只使用普通FTP)
- 织梦在广告(myad)中使用css样式
使用单引号,以及只有style这一个属性
- caffe-ubuntu1604-gtx850m-i7-4710hq----VGG_ILSVRC_16_layers.caffemodel
c++调用vgg16: ./build/install/bin/classification \ /media/whale/wsWin10/wsCaffe/model-zoo/VGG16//deplo ...
- 如何使用CocoaPods安装使用及配置私有库以及管理依赖库 【原创】
CocoaPods是什么 在iOS开发中势必会用到一些第三方依赖库,比如大家都熟悉的ASIHttpRequest.AFNetworking.JSONKit等.使用这些第三方类库能极大的方便项目的开发, ...