HDU 4825 Xor Sum (trie树处理异或)
Xor Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 3647 Accepted Submission(s): 1595
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
分析
要求求出原集合中与所给的数异或值最大的数。
建立一颗trie树,左边是0,右边是1。将所有数放到trie树中。
那么,对于给定的数x,将x二进制分解,在trie中从高位往低位走,每一位最好是和x不同(异或:不同为1,相同为0)。
code
#include<cstdio>
#include<algorithm>
#include<cstring> using namespace std;
const int N = ; inline char nc() {
static char buf[],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>''; ch=nc()) if(x=='-')f=-;
for (; ch>=''&&ch<=''; ch=nc()) x=x*+ch-'';
return x * f;
}
struct Trie{
int val[N],ch[N][];
int cnt;
void init() {
memset(ch,,sizeof(ch));
memset(val,,sizeof(val));
cnt = ;
}
void Insert(int x) {
int u = ;
for (int i=; i>=; --i) {
int c = (x&(<<i)) > ;
if (!ch[u][c]) ch[u][c] = ++cnt;
u = ch[u][c];
}
val[u] = x;
}
int Query(int x) {
int u = ;
for (int i=; i>=; --i) {
int c = (x&(<<i))>;
if (ch[u][!c]) u = ch[u][!c];
else if(ch[u][c]) u = ch[u][c]; //
}
return val[u];
}
}t; int main () {
int T = read();
for (int Case=; Case<=T; ++Case) {
t.init();
int n = read(),m = read();
for (int i=; i<=n; ++i) {
int a = read();
t.Insert(a);
}
printf("Case #%d:\n",Case);
while (m--) {
int a = read();
printf("%d\n",t.Query(a));
}
}
return ;
}
HDU 4825 Xor Sum (trie树处理异或)的更多相关文章
- hdu 4825 Xor Sum trie树
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Proble ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- HDU 4825 Xor Sum 字典树+位运算
点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- hdu 4825 xor sum(字典树+位运算)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- HDU 4825 Xor sum
trie树的异或和问题 本题是一道经典题,使用trie树维护所给出的集合,我们知道等比数列前n项的和比第n+1项小,所以本题可以使用贪心策略,对于每一个询问,我们从高位向低位匹配,寻找最大异或值,向下 ...
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- hdu 4825 Xor Sum (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...
- HDU4825 Xor Sum —— Trie树
题目链接:https://vjudge.net/problem/HDU-4825 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU 4825 Xor Sum (裸字典树+二进制异或)
题目链接 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将 ...
随机推荐
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第十四天(非原创)
文章大纲 一.淘淘商城总体架构介绍二.淘淘商城重要技术点总结三.项目常见面试题四.项目学习(all)资源下载五.参考文章 一.淘淘商城总体架构介绍 1. 功能架构 2. 技术选型 (1)Sprin ...
- mysql 批量修改 表字段/表/数据库 字符集和排序规则
今天接到一个任务是需要把数据库的字符编码全部修改一下,写了以下修正用的SQL,修正顺序是 表字段 > 表 > 数据库. 表字段修复: #改变字段数据 SELECT TABLE_SCHE ...
- Java的HashMap和HashTable(转)
来源:http://www.cnblogs.com/devinzhang/archive/2012/01/13/2321481.html 1. HashMap 1) hashmap的数据结构 Has ...
- 《敏捷软件开发:原则、模式与实践(C#版)》源代码下载
Agile Software Development: Principles, Patterns and Practice (C# Edition) Source Code 这本书的经典性无需多言 ...
- Teradata 认证系列 - 2. Teradata数据库总览
Teradata (以下简称TD) 总览本课的学习目标 描述Teradata数据库产品的功能 知晓支持的操作系统 描述Teradata的并行架构 解释线性可扩展性 列出Teradata DBA永远不需 ...
- 使用data-自定义数据及如何获取该值
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Cloneable接口的作用
Cloneable接口是一个[标记接口],就是没有任何内容 implements Cloneable表示该对象能被克隆,能使用Object.clone()方法.如果没有implements Clone ...
- meterpreter > ps
meterpreter > ps Process List============ PID PPID Name Arch Session User Path --- ---- ---- ---- ...
- windows下php7.1.5、mysql环境搭建
php http://windows.php.net/download/ 如果是使用ISAPI的方式来运行PHP就必须用Thread Safe(线程安全)的版本:而用FastCGI模式运行PHP的话就 ...
- Android(java)学习笔记90:TextView 添加超链接(两种实现方式)
1. TextView添加超链接: TextView添加超链接有两种方式,它们有区别于WebView: (1)方式1: LinearLayout layout = new LinearLayout(t ...