题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=4825

题目:

题意:

  先给你n个数,再进行q次查询,每次查询数s与原来给的n个数异或和最大的数。

思路:

  建一棵字典树,将n个数转换成二进制存进去,每次查询时,对s的每一位进行匹配(从高位开始匹配,毕竟你低位再大也没有一位高位大效果好,如34 <42),匹配第i位时为了值尽可能大应先匹配1-a[i]再匹配a[i]。

代码实现如下:

 #include <set>
#include <map>
#include <deque>
#include <ctime>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define name2str(name)(#name)
#define bug printf("**********\n");
#define IO ios::sync_with_stdio(false);
#define debug(x) cout<<#x<<"=["<<x<<"]"<<endl;
#define FIN freopen("/home/dillonh/CLionProjects/in.txt","r",stdin); const double eps = 1e-;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int t, n, q, le, root;
LL x;
int a[]; struct node{
int cnt;
int nxt[]; void init(){
cnt = ;
nxt[] = nxt[] = -;
}
}T[maxn*]; void insert(LL n){
int now = root;
for(int i = ; i <= ; i++) {
a[i] = n % ;
n /= ;
}
for(int i = ; i >= ; i--){
int x = a[i];
if(T[now].nxt[x] == -){
T[le].init();
T[now].nxt[x] = le++;
}
now = T[now].nxt[x];
T[now].cnt++;
}
} LL search(LL n){
int now = root;
LL ans = ;
for(int i = ; i <= ; i++) {
a[i] = n % ;
n /= ;
}
for(int i = ; i >= ; i--){
int x = a[i];
if(T[now].nxt[ - x] != -) {
if(x == ) ans += 1LL << i;
now = T[now].nxt[-x];
} else {
if(x == ) ans += 1LL << i;
now = T[now].nxt[x];
}
}
return ans;
} int main() {
int icase = ;
scanf("%d", &t);
while(t--) {
le = ;
T[].init();
scanf("%d%d", &n, &q);
for(int i = ; i <= n; i++) {
scanf("%lld", &x);
insert(x);
}
printf("Case #%d:\n", ++icase);
for(int i = ; i <= q; i++) {
scanf("%lld", &x);
printf("%lld\n", search(x));
}
}
return ;
}

Xor Sum(HDU4825 + 字典树)的更多相关文章

  1. HDU-4825 Xor Sum,字典树好题!

    Xor Sum 一遍A了之后大呼一声好(keng)题!debug了两小时~~~~百度之星资格赛,可以. 题意:给你一个n个元素的数组,m次查询,每次输入一个数k要求从数组中找到一个数与k异或值最大,输 ...

  2. HDU4825 Xor Sum(字典树解决最大异或问题)

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

  3. Xor Sum 01字典树 hdu4825

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  4. HDU-4825 Xor Sum(字典树求异或最大值)

    题目链接:点此 我的github地址:点此 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整 ...

  5. HDU 4825 Xor Sum(字典树)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 这道题更明确的说是一道01字典树,如果ch[u][id^1]有值,那么就向下继续查找/ ...

  6. hdu4825 字典树 XOR

    用字典树思想来做.对于一个数,给出他的二进制,然后更具二进制建立字典树,然后每次询问的时候的数也给出二进制,如果当前为1,那就向0走,为0,向1走. #include<stdio.h> # ...

  7. HDU--5269 ZYB loves Xor I (字典树)

    题目电波: HDU--5269 ZYB loves Xor I 首先我们先解决 ai xor aj 每个数转化为二进制  我们用字典树统计 每个节点 0 和 1 的出现的个数 #include< ...

  8. HDU 5715 XOR 游戏 二分+字典树

    XOR 游戏 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5715 Description 众所周知,度度熊喜欢XOR运算(XOR百科). 今天,它 ...

  9. HDU5715 XOR 游戏 二分+字典树+dp

    当时Astar复赛的时候只做出1题,赛后补题(很长时间后才补,懒真是要命),发现这是第二简单的 分析: 这个题,可以每次二分区间的最小异或和 进行check的时候用dp进行判断,dp[i][j]代表前 ...

随机推荐

  1. Java 文件下载功能 解决中文乱码

    Html部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...

  2. sourcetree git合并问题

    在使用sourcetree做多功能合并(合并不提交)的时候,有时按钮是灰色的,直接点击右上角命令行模式 git merge <branch1> --no-commit 转载请注明博客出处: ...

  3. java线程dump分析工具

    jstack和线程dump分析  java程序性能分析之thread dump和heap dump 一.[内存dump] jmap –dump:live,format=b,file=heap.bin ...

  4. tomcat设置虚拟路径映射服务器指定的物理路径

    在tomcat的server.xml中的host标签中加入如下标签: <Context crossContext="false" debug="1" do ...

  5. 网络对抗课题4.3.1 SQL注入原理与实践

    网络对抗课题4.3.1 SQL注入原理与实践 原理 SQL注入漏洞是指在Web应用对后台数据库查询语句处理存在的安全漏洞.也就是,在输入字符串中嵌入SQL指令,在设计程序中忽略对可能构成攻击的特殊字符 ...

  6. adb server version (32) doesn't match this client (36); killing...

    http://blog.csdn.net/seaker_/article/details/55107598 FAQ: adb server version (36) doesn't match thi ...

  7. sqoop 补充

    1.用 sqoop 将MySQL中的数据导入hbase中 sqoop import \--connect jdbc:mysql://***.***.*.***:3306/mysql \--hbase- ...

  8. Hbase(七)hbase高级编程

    一.Hbase结合mapreduce 为什么需要用 mapreduce 去访问 hbase 的数据?     ——加快分析速度和扩展分析能力     Mapreduce 访问 hbase 数据作分析一 ...

  9. 仿微博的JQuery日历控件

    实现原理主要是处理table,生成tr td,其中最重要的是如何找出每月第一天是星期几,然后就能对应出这个月的余下天数. 日历控件网上一搜一大把,但是我觉得自己写一遍还是有好处的.代码可以查看本页源代 ...

  10. hiho一下 第197周 逆序单词

    1.set #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath&g ...