题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825

题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大,输出y;

把已知序列建立01字典树,然后查找即可;就是把十进制数x装换成二进制01,因为数在int范围内,所以可以前补零构成32位,按顺序插入即可;

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define oo 1000000000000000
#define N 102550
#define PI 4*atan(1.0)
#define mod 100000001
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL; struct node
{
int num;
node *Next[];
}; void Add(node *head, int num)
{
node *p = head;
for(int i=; i>=; i--)
{
int k = (num>>i)&;
if(p->Next[k] == NULL)
{
node *q = new node();
p->Next[k] = q;
}
p = p->Next[k];
}
p->num = num;
} int Find(node *head, int num)
{
node *p = head;
for(int i=; i>=; i--)
{
int k = (num>>i)&;
if(p->Next[k^] != NULL)
p = p->Next[k^];
else
p = p->Next[k];
}
return p->num;
} int main()
{
int T, t = , n, m, num;
scanf("%d", &T);
while(T--)
{
node *head = new node(); scanf("%d %d", &n, &m);
for(int i=; i<=n; i++)
{
scanf("%d", &num);
Add(head, num);
} printf("Case #%d:\n", t++); for(int i=; i<=m; i++)
{
scanf("%d", &num);
int ans = Find(head, num);
printf("%d\n", ans);
}
}
return ;
}

Xor Sum---hdu4825(01字典树模板)的更多相关文章

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

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

  2. 2014百度之星资格赛—— Xor Sum(01字典树)

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

  3. HDU 4825 Xor Sum(01字典树入门题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...

  4. hdu 4825 Xor Sum(01字典树模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...

  5. HDU 4825 Xor Sum(01字典树)题解

    思路:先把所有数字存进字典树,然后从最高位贪心. 代码: #include<set> #include<map> #include<stack> #include& ...

  6. hdu4825 01字典树+贪心

    从高位向低位构造字典树,因为高位得到的数更大. AC代码: #include<cstdio> using namespace std; typedef long long LL; cons ...

  7. HDU 4825 Xor Sum (裸字典树+二进制异或)

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

  8. Vasiliy's Multiset CodeForces -706D || 01字典树模板

    就是一个模板 注意这题有一个要求:有一个额外的0一直保持在集合中 #include<cstdio> #include<algorithm> using namespace st ...

  9. HDU 4825 Xor Sum (模板题)【01字典树】

    <题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...

随机推荐

  1. Codeforces Round #208 (Div. 2) B Dima and Text Messages

    #include <iostream> #include <algorithm> #include <string> using namespace std; in ...

  2. Xcode 编辑时的爆炸效果

    Xcode 的爆炸效果 1.打开终端输入 $mkdir -p ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins;$curl ...

  3. 【wikioi】1022 覆盖(匈牙利)

    http://www.wikioi.com/problem/1022/ 好不容易来一次1A,,水题啊.. 染色后裸匈牙利orz #include <cstdio> #include < ...

  4. 【BZOJ】1491: [NOI2007]社交网络(floyd)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1491 囧囧囧...................... 囧1:虽然自己想到做法了,但是操作的时候, ...

  5. zabbix自定义键值原理

    子配置文件的配置 为了便于维护和分类管理,UserParameter的内容可以单独写一个配置文件 # vim /usr/local/zabbix/etc/zabbix_agentd.conf Incl ...

  6. js将日期格式转换为YYYY-MM-DD HH:MM:SS

    <script language="javascript" type="text/javascript"> //页面加载 jQuery(docume ...

  7. HDU 4825 Xor Sum(经典01字典树+贪心)

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

  8. CodeForces 86D Powerful array(莫队+优化)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  9. Jenkins学习记录

    参考资料 官方文档 用MSBuild和Jenkins搭建持续集成环境(1) 用MSBuild和Jenkins搭建持续集成环境(2) 构建基于Jenkins + Github的持续集成环境 Jenkin ...

  10. UITableview xib里面 cell 按钮的回调

    //  MoreBtnCell.m#import <UIKit/UIKit.h> @interface MoreBtnCell : UITableViewCell @property (w ...