HDU 4825

对于给定的查询(一个整数),求集合中和他异或值最大的值是多少

按位从高位往低位建树,查询时先将查询取反,然后从高位往低位在树上匹配,可以匹配不可以匹配都走同一条边(匹配表示有一个异或值为1的边,选择当然最好;不能匹配说明不存在一条异或值为1的边,那么只存在一条为0的边,也不得不选)

 //#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, (L + R)>>1
#define rson k<<1|1, ((L + R)>>1) + 1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define dec(i, a, b) for(int i = a; i >= b; i --) template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = + ;
const int MAXM = ;
const double eps = 1e-;
LL MOD = ; const LL H = ((LL) << ) - ;
int t, n, m, num[], cas = ;
int tree[][], s; void get_num(LL x) {
mem0(num);
int id = ;
while(x) {
num[id++] = x % ;
x >>= ;
}
} void insert_to_tree(LL x)
{
get_num(x);
int u = ;
dec (i, , ) {
int c = num[i];
if(!tree[u][c]) {
tree[u][c] = ++s;
}
u = tree[u][c];
}
} int main()
{
// FIN;
cin >> t;
while(t--) {
s = ;
mem0(tree);
scanf("%d %d", &n, &m);
LL x, ans;
rep (i, , n) {
scanf("%lld", &x);
insert_to_tree(x);
}
printf("Case #%d:\n", ++cas);
rep (i, , m) {
scanf("%lld", &x);
x = (~x) & H;
get_num(x);
int u = ;
ans = ;
dec (j, , ) {
int c = num[j];
ans |= (LL)(tree[u][c] ? c : !c) << j;
u = tree[u][c] ? tree[u][c] : tree[u][!c];
}
printf("%lld\n", ans);
}
}
return ;
}

HDU 4825 字典树的更多相关文章

  1. Chip Factory HDU - 5536 字典树(删除节点|增加节点)

    题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 ("^&qu ...

  2. HDU 5687 字典树插入查找删除

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...

  3. HDU 5384 字典树、AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...

  4. hdu 2112(字典树+最短路)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. hdu 2072(字典树模板,set,map均可做)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...

  6. hdu 1251 字典树的应用

    这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include < ...

  7. hdu 2896 字典树解法

    #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> ...

  8. Repository HDU - 2846 字典树

    题意:给出很多很多很多很多个 单词 类似搜索引擎一下 输入一个单词 判断有一个字符串包含这个单词 思路:字典树变体,把每个单词的后缀都扔字典树里面,这里要注意dd是一个单词 但是把d 和dd都放字典树 ...

  9. Phone List HDU - 1671 字典树

    题意:给出一堆一组一组的数字  判断有没有哪一个是另外一个的前缀 思路:字典树 插入的同时进行判断  不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含 刚开始 ...

随机推荐

  1. MyBtis—原理及初始化

    Mybatis的功能架构分为三层: 1)       API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库.接口层 一接收到调用请求就会调用数据处理层来完成具体的数据处理. ...

  2. 【运维技术】CentOS7上从零开始安装LAMP安装织梦DedeCMS教程

    前期准备数据 centos7 系统 安装 appache httpd # 更新httpd yum update httpd # 安装httpd yum install -y httpd # 启动服务 ...

  3. python排序算法实现(冒泡、选择、插入)

    python排序算法实现(冒泡.选择.插入) python 从小到大排序 1.冒泡排序: O(n2) s=[3,4,2,5,1,9] #count = 0 for i in range(len(s)) ...

  4. Android -- 在一个Activity开启另一个Activity 并 获取他的返回值。

    1. 视图示例, 按选择弹出 2界面, 选择选项  回显到1    2. 示例代码 MainActivity.java, 第一个activity public class MainActivity e ...

  5. Gradle sync failed: Read timed out

    : Gradle sync started : Gradle sync failed: Read timed out Consult IDE log m s ms) 原因是Gradle下载超时 一.下 ...

  6. Ubuntu16 nginx安装http_image_filter_module模块

    目录 配置image_filter 配置 重启nginx 如何安装呢? 安装image_filter模块依赖的库. 查看之前的配置 添加上图片模块[由于它是系统模块,不需要额外下载,直接添加就可以了] ...

  7. Miller_Rabin(米勒拉宾)素数测试

    2018-03-12 17:22:48 米勒-拉宾素性检验是一种素数判定法则,利用随机化算法判断一个数是合数还是可能是素数.卡内基梅隆大学的计算机系教授Gary Lee Miller首先提出了基于广义 ...

  8. iTerm2的设置和Zsh.

    很好的说明文: https://xiaozhou.net/learn-the-command-line-iterm-and-zsh-2017-06-23.html iTerm2是Mac os用户使用的 ...

  9. MyBatis Generator去掉生成的注解

    是不是很讨厌mybatis Generator帮我们生成代码的时候在Mapper和mapper.xml文件中生成的一大堆注解?今天在看MyBatis Generator代码的时候发现,原来mybati ...

  10. IE6开发调试插件:IE Developer Toolbar

    下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=18359 1.下载后点击安装 2.安装后重启IE6