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

Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大。Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助。你能证明人类的智慧么?
 

Input

输入包含若干组测试数据,每组测试数据包含若干行。
输入的第一行是一个整数T(T < 10),表示共有T组数据。
每组数据的第一行输入两个正整数N,M(<1=N,M<=100000),接下来一行,包含N个正整数,代表 Zeus 的获得的集合,之后M行,每行一个正整数S,代表 Prometheus 询问的正整数。所有正整数均不超过2^32。
 

Output

对于每组数据,首先需要输出单独一行”Case #?:”,其中问号处应填入当前的数据组数,组数从1开始计算。
对于每个询问,输出一个正整数K,使得K与S异或值最大。
 

Sample Input

2
3 2
3 4 5
1
5
4 1
4 6 5 6
3
 

Sample Output

Case #1:
4
3
Case #2:
4
 

Source

 
按照集合里每个数的二进制建立01字典树,询问时,要想结果最大,走树上反着的那条边。
 //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字典树)的更多相关文章

  1. hdu4825 01字典树+贪心

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

  2. hdu-4825(01字典树)

    题意:中文题意 解题思路:01字典树板子题 代码: #include<iostream> #include<algorithm> #include<cstdio> ...

  3. cf842D 01字典树|线段树 模板见hdu4825

    一般异或问题都可以转换成字典树的问题,,我一开始的想法有点小问题,改一下就好了 下面的代码是逆向建树的,数据量大就不行 /*3 01字典树 根据异或性质,a1!=a2 ==> a1^x1^..^ ...

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

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

  5. [HDU-4825] Xor-Sum (01字典树)

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

  6. 数据结构&字符串:01字典树

    利用01字典树查询最大异或值 01字典树的是只含有0和1两种字符的字典树,在使用它的时候,把若干数字转成二进制后插入其中 在查询树中的哪个数字和给定数字有最大异或值的时候,从根开始贪心查询就ok了 H ...

  7. Chip Factory---hdu5536(异或值最大,01字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:有一个数组a[], 包含n个数,从n个数中找到三个数使得 (a[i]+a[j])⊕a[k] ...

  8. Xor Sum---hdu4825(01字典树模板)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...

  9. Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树

    A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...

随机推荐

  1. 学习tableauhttps://www.tableau.com/zh-cn/learn/training

    入门教程不错: https://www.tableau.com/zh-cn/learn/training

  2. ReactNative学习笔记(五)踩坑总结

    已经发现的bug或者问题 Android不支持shadow属性: Animated.Image的borderRadius不生效: setNativeProps无法修改图片的source: 没有直接设置 ...

  3. 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互

    [源码下载] 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互 作者: ...

  4. 2019-4-22 jdbc学习笔记

    jdbc 一.定义:java database connector 二.常用的接口 java.sql.Driver  驱动 java.sql.Connection  链接 java.sql.State ...

  5. OCP 12c考试题,062题库出现大量新题-第20道

    choose three Your database is configured for ARCHIVELOG mode, and a daily full database backup is ta ...

  6. Rocketmq日志收集与logback集成Demo

    官方文档有简洁的例子,这里就做一个简单补充和实践 直接上logback-boot.xml文件 <?xml version="1.0" encoding="UTF-8 ...

  7. 程序性能调优工具之gprob

    1 简介改进应用程序的性能是一项非常耗时耗力的工作,但是究竟程序中是哪些函数消耗掉了大部分执行时间,这通常都不是非常明显的.GNU 编译器工具包所提供了一种剖析工具 GNU profiler(gpro ...

  8. Linux - 查看文件信息的三个命令

    ls命令 - list directory contents 显示文件详细信息:ls -l <file name> file命令 - determine file type determi ...

  9. # postgresql-shared_buffers

    关于shared_buffers 什么是shred_buffer,我们为什么需要shared_buffers? 1.在数据库系统中,我们主要关注磁盘io,大多数oltp工作负载都是随机io,因此从磁盘 ...

  10. SpringBoot初探(上传文件)

    学了Spring,SpringMVC,Mybatis这一套再来看SpringBoot,心里只有一句握草,好方便 这里对今天做的东西做个总结,然后在这之间先安利一个热部署的工具,叫spring-DevT ...