题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825

题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大,输出y;

把已知序列建立01字典树,然后查找即可;就是把十进制数x装换成二进制01,因为数在int范围内,所以可以前补零构成32位,按顺序插入即可;

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<string.h>
  4. #include<stdio.h>
  5. #include<math.h>
  6. #include<vector>
  7. using namespace std;
  8. #define INF 0x3f3f3f3f
  9. #define oo 1000000000000000
  10. #define N 102550
  11. #define PI 4*atan(1.0)
  12. #define mod 100000001
  13. #define met(a, b) memset(a, b, sizeof(a))
  14. typedef long long LL;
  15.  
  16. struct node
  17. {
  18. int num;
  19. node *Next[];
  20. };
  21.  
  22. void Add(node *head, int num)
  23. {
  24. node *p = head;
  25. for(int i=; i>=; i--)
  26. {
  27. int k = (num>>i)&;
  28. if(p->Next[k] == NULL)
  29. {
  30. node *q = new node();
  31. p->Next[k] = q;
  32. }
  33. p = p->Next[k];
  34. }
  35. p->num = num;
  36. }
  37.  
  38. int Find(node *head, int num)
  39. {
  40. node *p = head;
  41. for(int i=; i>=; i--)
  42. {
  43. int k = (num>>i)&;
  44. if(p->Next[k^] != NULL)
  45. p = p->Next[k^];
  46. else
  47. p = p->Next[k];
  48. }
  49. return p->num;
  50. }
  51.  
  52. int main()
  53. {
  54. int T, t = , n, m, num;
  55. scanf("%d", &T);
  56. while(T--)
  57. {
  58. node *head = new node();
  59.  
  60. scanf("%d %d", &n, &m);
  61. for(int i=; i<=n; i++)
  62. {
  63. scanf("%d", &num);
  64. Add(head, num);
  65. }
  66.  
  67. printf("Case #%d:\n", t++);
  68.  
  69. for(int i=; i<=m; i++)
  70. {
  71. scanf("%d", &num);
  72. int ans = Find(head, num);
  73. printf("%d\n", ans);
  74. }
  75. }
  76. return ;
  77. }

Xor Sum---hdu4825(01字典树模板)的更多相关文章

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

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

  2. 2014百度之星资格赛—— Xor Sum(01字典树)

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

  3. HDU 4825 Xor Sum(01字典树入门题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...

  4. hdu 4825 Xor Sum(01字典树模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...

  5. HDU 4825 Xor Sum(01字典树)题解

    思路:先把所有数字存进字典树,然后从最高位贪心. 代码: #include<set> #include<map> #include<stack> #include& ...

  6. hdu4825 01字典树+贪心

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

  7. HDU 4825 Xor Sum (裸字典树+二进制异或)

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

  8. Vasiliy's Multiset CodeForces -706D || 01字典树模板

    就是一个模板 注意这题有一个要求:有一个额外的0一直保持在集合中 #include<cstdio> #include<algorithm> using namespace st ...

  9. HDU 4825 Xor Sum (模板题)【01字典树】

    <题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...

随机推荐

  1. C# 如何判断数据是否为 NaN

    double a = 0 / 0d; if (double.IsNaN(a)){ //do } 在浮点数计算中, 0除以0将得到NaN ,正数除以0将得到PositiveInfinity ,负数除以0 ...

  2. javascript中字符串常用操作总结、JS字符串操作大全

    字符串的操作在js中非常频繁,也非常重要.以往看完书之后都能记得非常清楚,但稍微隔一段时间不用,便会忘得差不多,记性不好是硬伤啊...今天就对字符串的一些常用操作做个整理,一者加深印象,二者方便今后温 ...

  3. 【wikioi】1116 四色问题

    题目链接 算法:DFS 刚开始卡了一下,但后面想了想,于是 放上代码: #include <iostream> using namespace std; bool map[9][9]; i ...

  4. HDU 4057 Rescue the Rabbit(AC自动机+DP)

    题目链接 一个数组开小了一点点,一直提示wa,郁闷,这题比上个题简单一点. #include <iostream> #include <cstring> #include &l ...

  5. Redis内存存储结构分析

    1 Redis 内存存储结构 本文是基于 Redis-v2.2.4 版本进行分析. 1.1 Redis 内存存储总体结构 Redis 是支持多key-value数据库(表)的,并用 RedisDb 来 ...

  6. C# 使用 GetOleDbSchemaTable 检索架构信息(表、列、主键等)

    本文演示如何用 ADO.NET 中 OleDbConnection 对象的 GetOleDbSchemaTable 方法检索数据库架构信息.数据源中的架构信息包括数据库或可通过数据库中的数据源.表和视 ...

  7. git 创建branch分支

    开发者user1 负责用getopt 进行命令解析的功能,因为这个功能用到getopt 函数,于是将这个分支命名为user1/getopt.(1)确保是在开发者user1的工作区中cd /home/j ...

  8. bin/bash 和 /bin/sh 的区别

    今天在用ssh Secure shell 连接虚拟机中的Ubuntu编写程序时,想比对一下两个源代码有什么差别,但是在一个ssh 客户端下不断的切换很是费劲.于是想着在主机中再添加一个用户.我原本用s ...

  9. Shell下的正则表达式 (鸟哥私房菜)

    "Open Source" is a good mechanism to develop programs.$ apple is my favorite food.$ Footba ...

  10. switch parser.p4源码

    /* Copyright 2013-present Barefoot Networks, Inc. Licensed under the Apache License, Version 2.0 (th ...