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 ...
随机推荐
- Docker基础-使用Dockerfile创建镜像
1.基本结构 Dockerfile由一行行命令语句组成,并支持以#开头的注释行.例如: # This dockerfile uses the ubuntu image # VERSION 2 - ED ...
- hadoop2.4.0伪分布式搭建以及分布式关机重启后datanode没起来的解决办法
1.准备Linux环境 1.0点击VMware快捷方式,右键打开文件所在位置 -> 双击vmnetcfg.exe -> VMnet1 host-only ->修改subnet ip ...
- 595. Big Countries --- SQL related from leetcode
595. Big Countries There is a table World +-----------------+------------+------------+------------- ...
- Mac 视频录制然后转 gif
https://gist.github.com/dergachev/4627207 用 ScreenShot 或 Quicktime Player 录制视频, 保存位 in.mov ffmpeg -i ...
- Android 开发工具方法整理
1. 获取当前手机系统语言 Locale.getDefault().getLanguage(); 2. 获取当前手机系统版本号 android.os.Build.VERSION.RELEASE; 3. ...
- 阿里开源项目arthas安装使用
文档地址 https://alibaba.github.io/arthas/install-detail.html 开始安装 我本地就装window版本了,下载zip包 按照快速入门,编译demo程序 ...
- ubuntu root默认密码(初始密码)
ubuntu安装好后,root初始密码(默认密码)不知道,需要设置. 1.先用安装时候的用户登录进入系统 2.输入:sudo passwd 按回车 3.输入新密码,重复输入密码,最后提示passwd ...
- C# 获取Header中的token值
public CurrentUser currentUser { get { CurrentUser result = new CurrentUser(); //jwt 解密token IJsonSe ...
- python 旋转数组 多种解题思路
leetcode 题目描述:给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题. 要求使用空间复杂度为 O(1) 的 ...
- 【sping揭秘】16、@After(finally) 但是这个实在afterturning之前执行
package cn.cutter.start.bean; import org.apache.commons.logging.Log; import org.apache.commons.loggi ...