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 ...
随机推荐
- python持久化
将对象转为二进制字节流(pickle) import pickle my_list = [1,2,3] pickle_file = open('my_list.pkl', 'wb') #注意二进制写入 ...
- 卷积在深度学习中的作用(转自http://timdettmers.com/2015/03/26/convolution-deep-learning/)
卷积可能是现在深入学习中最重要的概念.卷积网络和卷积网络将深度学习推向了几乎所有机器学习任务的最前沿.但是,卷积如此强大呢?它是如何工作的?在这篇博客文章中,我将解释卷积并将其与其他概念联系起来,以帮 ...
- 学习tableauhttps://www.tableau.com/zh-cn/learn/training
入门教程不错: https://www.tableau.com/zh-cn/learn/training
- Spring 使用javaconfig配置aop
1.在xml中需要配置自动代理 /** * */ package com.junge.demo.spring.dao; import org.springframework.context.annot ...
- 压测工具之JMeter之环境配置及运行
一.下载JMeter 地址:http://jmeter.apache.org/download_jmeter.cgi 系统:win7 x64 在 Binaries 目录下选择一个合适自己系统的版本,不 ...
- Avoiding Common Networking Mistakes
https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/Networkin ...
- Android核心技术Intent和数据存储篇
女孩:上海站到了? 男孩:嗯呢?走向世界~ 女孩:Intent核心技术和数据存储技术? 男孩:对,今日就讲这个~ Intent是各个组件之间用来进行通信的,Intent的翻译为"意图&quo ...
- 用react + redux + router写一个todo
概述 最近学习redux,打算用redux + router写了一个todo.记录下来,供以后开发时参考,相信对其他人也有用. 注意: 我只实现了Footer组件的router,其它组件的实现方法是类 ...
- vue环境安装
node.js安装 https://nodejs.org/en/ cnpm安装 npm install -g cnpm --registry=https://registry.npm.taobao.o ...
- java 浅谈web系统当中的cookie和session会话机制
一 Cookie: 1. Cookie翻译为小甜饼,有一种特殊的味道.cookie主要用来在(浏览器)客户端做记号用的.Cookie不属于java,Cookie是一种通用的机制,属于HTTP协议的一部 ...