题意

给定一个集合后, 求一组查询中每个数和集合中任一元素异或的最大值.

题解

异或的规律是这样的 1 ^ 1 = 0, 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 而最大值即是代表了, 在 靠前的位置 上有 **尽量多的 1 **. 因此, 对于答案来说, 等价于靠前的位置 上有 尽量与查询数值对应位不相同的数字

这类题目可以用 01TrieO(1) 的时间内完成对 一个查询 的求解.

具体思路: 对于一个数字, 首先取反(也可以不取反, 只不过后续判断条件会有稍微变化), 从 高位到低位 依次遍历 指向 0 所代表的子树 的指针指向 1 所代表的子树 的指针 是否为 NULL, 如果不为空则继续下一层, 否则就向当前节点的另一个分支遍历.

AC代码

#include <cstdio>
#include <iostream>
using namespace std;
const int ARRSIZE = 2;
struct TrieNode {
TrieNode* next[ARRSIZE];
TrieNode() {
for(int i = 0; i < ARRSIZE; i++)
next[i] = NULL;
}
};
void insertNum(TrieNode* root, unsigned num) {
TrieNode* p = root;
for(int i = 31; i >= 0; i--) {
int bit = (num >> i) & 1;
// cout << " bit is " << bit;
if(!p->next[bit])
p->next[bit] = new TrieNode();
p = p->next[bit];
}
// cout << endl;
}
int searchNum(TrieNode* root, int num) {
num = ~num;
int ret = 0;
TrieNode* p = root;
for(int i = 31; i >= 0; i--) {
int index = (num >> i) & 1; if(!p->next[index])
index = 1 - index;
ret += (index << i);
p = p->next[index];
}
return ret;
}
int main() {
int nTest; scanf("%d", &nTest);
for(int t = 1; t <= nTest; t++) {
printf("Case #%d:\n", t);
TrieNode* root = new TrieNode();
int nNum, nQuery;
scanf("%d %d", &nNum, &nQuery);
while(nNum--) {
unsigned num; scanf("%u", &num);
insertNum(root, num);
}
while(nQuery--) {
int tar; scanf("%u", &tar);
printf("%u\n", searchNum(root, tar));
}
}
return 0;
}

HDU4825 Xor Sum的更多相关文章

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

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

  2. HDU4825 Xor Sum —— Trie树

    题目链接:https://vjudge.net/problem/HDU-4825 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

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

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

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

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

  5. ☆ [HDU4825] Xor Sum「最大异或和(Trie树)」

    传送门:>Here< 题意:给出一个集合,包含N个数,每次询问给出一个数x,问x与集合中的一个数y异或得到最大值时,y是多少? 解题思路 由于N,M非常大,暴力显然不行.抓住重点是异或,所 ...

  6. HDU4825 Xor Sum (01Trie)

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

  7. [Hdu4825]Xor Sum(01字典树)

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

  8. HDU4825:Xor Sum 解题报告(0/1 Trie树)

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

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

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

随机推荐

  1. docker 安装ElasticSearch的中文分词器IK

    首先确保ElasticSearch镜像已经启动 安装插件 方式一:在线安装 进入容器 docker exec -it elasticsearch /bin/bash 在线下载并安装 ./bin/ela ...

  2. tr设置display属性时,在FF中td合并在第一个td中显示的问题

      今天用firefox测试页面的时候,发现用javascript控制 tr 的显示隐藏时,当把tr的显示由“display:none”改为“display:block”时,该tr下的td内容合并到了 ...

  3. jQuery中的pushStack

    在学习jquery源码的时候,学到了其中的pushStack方法,在这里记录一下 源码为 // Take an array of elements and push it onto the stack ...

  4. css-css的基本选择器(三种)

    ** 要对哪个标签里面的数据进行操作 (1)标签选择器 div { background-color:red; color:blue; } (2)class选择器 * 每个HTML标签中都有一个属性 ...

  5. git 命令收藏

    git init # 初始化本地git仓库(创建新仓库)   git config --global user.name "xxx" # 配置用户名   git config -- ...

  6. Activiti学习之HelloWorld程序

    流程图 部署流程定义 /** * 部署流程定义 */ @Test public void deploymentProcessDefinition() { ProcessEngine processEn ...

  7. c++中%是什么意思?

    两种意思:1.格式化字符串输出2.整数取余 1.目前printf支持以下格式的输出,例如:printf("%c",a):输出单个字符.printf("%d",a ...

  8. Raspberry 安装vstudio

    Visual Studio Code微软公司推出的一款轻量级的Visual Studio风格的跨平台的IDE.当然,除了Windows,OSX,还能在树莓派上使用.目前树莓派上可用的IDE真不多,VS ...

  9. ie6 浏览器的bug

    1.IE6不支持连续类的交集选择器 1 #box.box.box1{ 2             width: 200px; 3             height: 200px; 4       ...

  10. 【转】iOS lame编译 arm64 armv7s armv7 x86_64 i386指令集

    原文出至 http://blog.csdn.net/vieri_ch/article/details/40650467 最近升级了系统到Mac OS X 10.10 并且更新了XCode6.1和iOS ...