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 ...
随机推荐
- java基础篇1之可变参数,增强for循环,自动装箱
1.java中可变参数应用 例如 add(int x,int... args) 1)只能放在参数列表的最后面 2)...位于变量类型和变量名之间,前后有无空格都可以 3)调用可变参数的方法时,编译器为 ...
- android MPChart图标使用具体解释
近期项目里有要加入更加复杂的图标了,曾经一些简单的曲线图,饼状图.风险指示图等,都是自己画.随着难度的添加.越来越力不从心.曾经研究过achartenginee图标框架,但发现achartengine ...
- Android Studio中利用JavaDoc生成项目API文档
1. 在Android Studio中的菜单项中点击Generate JavaDoc
- JAVA Eclipse创建Android程序界面不显示怎么办
一般是由于你创建的Android应用程序版本太高导致的,请设置4或以下版本,对于已有的项目,可以在属性-Android中修改目标生成的版本号 ...
- PHP提权之异步执行
在服务器上都会定时运行一些脚本以完成周期性的任务. 而这些脚本往往是以root权限启动的, 替换或者改变其中的内容就可以完成提权.而今天在这要讲解的就是php提权中的异步执行方法. 在php中一般大家 ...
- same-tree——比较两个二叉树是否相同
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 简单记录一次ORA-00600: internal error code, arguments: [2662]
接上一个,REDO报错搞定后OPEN数据库时又报错ORA-00600: internal error code, arguments: [2662]. 原因是_ALLOW_RESETLOGS_CORR ...
- C# 调用API接口处理公共类 自带JSON实体互转类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- C#各种导入Excel文件的数据的方法总结
在导入前都需要将上传的文件保存到服务器,所以避免重复的写这些代码,先贴出上传文件并保存到服务器指定路径的代码 protected void btnImport_Click(object sender, ...
- Linux系统rootpassword改动
重新启动系统. 进入系统引导界面: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzMzOTg1MQ==/font/5a6L5L2T/fontsi ...