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基础篇6之代理
代理的概念与应用 Class clazzProxy = Proxy.getProxyClass(Collection.class.getClassLoader,Collection.class); c ...
- Web开发人员不容错过的10个HTML5工具
HTML5已经成为当今世界的一个必定组成部分.由于World Wide Web万维网是使用超文本标记语言来架构和呈现的,于是HTML5成为了最流行的编程语言之中的一个.随着网络的不断扩张,Web开发者 ...
- Node.js学习入门手册
Node.js 安装 1.下载http://nodejs.org/dist/v0.12.1/node-v0.12.1-x86.msi并完成安装 2.下载https://www.python.org/f ...
- intent传值传对象跳转
intent传值传对象跳转 1.传值 //原activity中存入一个字段 intent = new Intent(From.this, To.class); intent.putExtra(&quo ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- Docker 三大核心工具
Docker-machineDocker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker,比如VirtualBox. Digital Oc ...
- 用递归法将一个整数n转换成字符串。
用递归法将一个整数n转换成字符串. 比如,输入483,应输出字符串"483".n的位数不确定,能够是随意位数的整数. #include "stdafx.h" # ...
- Cocos2d-x 更改文字换行风格 ( cocos2dx change line )
Cocos2dx change line 在 cocos2dx change line 的实现中,我们能够简单的使用 dimensions属性控制换行.使用它仅仅需将相应的參数值传入构造函数,或者调用 ...
- Getting Started with the G1 Garbage Collector(译)
原文链接:Getting Started with the G1 Garbage Collector 概述 目的 这篇教程包含了G1垃圾收集器使用和它如何与HotSpot JVM配合使用的基本知识.你 ...
- JS中的call_user_func封装
PHP常见的call_user_func方法,在JS中有时候会用到,比如你想根据某个动态变量去执行方法. 以前遇到过类似的问题没有解决,现在不太记得具体案例了.今天无意中看到类似文章,学到了.代码如下 ...