HDU4825(01字典树)
Xor Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 2920 Accepted Submission(s): 1264
Problem Description
Input
输入的第一行是一个整数T(T < 10),表示共有T组数据。
每组数据的第一行输入两个正整数N,M(<1=N,M<=100000),接下来一行,包含N个正整数,代表 Zeus 的获得的集合,之后M行,每行一个正整数S,代表 Prometheus 询问的正整数。所有正整数均不超过2^32。
Output
对于每个询问,输出一个正整数K,使得K与S异或值最大。
Sample Input
3 2
3 4 5
1
5
4 1
4 6 5 6
3
Sample Output
4
3
Case #2:
4
Source
//2017-09-16
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ; int n, m;
int trie[*N][], value[*N], tot; void init(){
tot = ;
memset(trie[], , sizeof(trie[]));
} void insert(int x){
int cur = ;
for(int i = ; i >= ; i--){
int idx = ((x>>i)&);
if(!trie[cur][idx]){
memset(trie[tot], , sizeof(trie[tot]));
value[tot] = ;
trie[cur][idx] = tot++;
}
cur = trie[cur][idx];
}
value[cur] = x;
} int query(int x){
int cur = ;
for(int i = ; i >= ; i--){
int idx = ((x>>i)&);
if(trie[cur][idx^]) cur = trie[cur][idx^];
else cur = trie[cur][idx];
}
return value[cur];
} int main()
{
int T, kase = ;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
init();
int x;
for(int i = ; i <= n; i++){
scanf("%d", &x);
insert(x);
}
printf("Case #%d:\n", ++kase);
while(m--){
scanf("%d", &x);
printf("%d\n", query(x));
}
} return ;
}
HDU4825(01字典树)的更多相关文章
- hdu4825 01字典树+贪心
从高位向低位构造字典树,因为高位得到的数更大. AC代码: #include<cstdio> using namespace std; typedef long long LL; cons ...
- hdu-4825(01字典树)
题意:中文题意 解题思路:01字典树板子题 代码: #include<iostream> #include<algorithm> #include<cstdio> ...
- cf842D 01字典树|线段树 模板见hdu4825
一般异或问题都可以转换成字典树的问题,,我一开始的想法有点小问题,改一下就好了 下面的代码是逆向建树的,数据量大就不行 /*3 01字典树 根据异或性质,a1!=a2 ==> a1^x1^..^ ...
- [Hdu4825]Xor Sum(01字典树)
Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问 ...
- [HDU-4825] Xor-Sum (01字典树)
Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...
- 数据结构&字符串:01字典树
利用01字典树查询最大异或值 01字典树的是只含有0和1两种字符的字典树,在使用它的时候,把若干数字转成二进制后插入其中 在查询树中的哪个数字和给定数字有最大异或值的时候,从根开始贪心查询就ok了 H ...
- Chip Factory---hdu5536(异或值最大,01字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:有一个数组a[], 包含n个数,从n个数中找到三个数使得 (a[i]+a[j])⊕a[k] ...
- Xor Sum---hdu4825(01字典树模板)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
随机推荐
- <Listener>HttpSessionListener和HttpSessionAttributeListener区别
一.HttpSessionListener HttpSessionListener是对Session的一个监听,主要监听关于Session的两个事件,即初始化和销毁.HttpSessionListen ...
- 文件描述符fd、文件指针fp和vfork()
1. fd:在形式上是一个非负整数.实际上他是一个索引值.指向kernal为每一个进程所维护的该进程打开文件的记录表. 当程序打开一个文件或者创建一个新文件的时候kernal向进程返回一个文件描述符. ...
- Android NDK学习(四):C/C++层调用JAVA
一.从C/C++层调用JAVA层代码(无参数调用) //在c代码里面调用java代码里面的方法 // java 反射 // 1 . 找到java代码的 class文件 // jclass (*Find ...
- shell 中的三种引号的作用
1. 单引号(' ') 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的: 单引号字串中不能出现单独一个的单引号(对单引号使用转义符后也不行),但可成对出现,作为字符串拼接使用. 2. 双 ...
- Maven - 实例-6-聚合与继承
创建项目 xxx - 继承自testDep.PPP <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi= ...
- Testing - 软件测试知识梳理 - 测试用例
测试用例 是指对一项特定的软件产品进行测试任务的描述,体现测试方案.方法.技术和策略. 内容包括测试目标.测试环境.输入数据.测试步骤.预期结果.测试脚本等,并形成文档. 每个具体测试用例都将包括下列 ...
- Python - 集成开发环境Pycharm的使用方法和技巧
PyCharm HomePage:PyCharm 我的Pycharm,我做主 Getting Started with PyCharm Pycharm使用技巧 Documentation & ...
- 自定义线程池reject策略
import java.util.Date;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.Rej ...
- python3模块: os
简介 os模块主要用于提供系统高级别的操作. 常用方法 os.access(path, mode) # 检验权限模式 os.chdir(path) # 改变当前工作目录 os.chflags(path ...
- Python函数——装饰器
前言 给下面的函数加上运行时间 def fib(n): a, b = 0, 1 for i in range(n): print(b) a, b = b, a+b return b a = fib(5 ...