题目链接:http://codeforces.com/contest/706/problem/D

题意很简单不多说。

把一个数当作二进制插入字典树中,按照xor的性质,1找0,0找1,贪心即可。

注意的是一开始集合中就有0,所以一开始'?'查询时输出他本身。

  1. //#pragma comment(linker, "/STACK:102400000, 102400000")
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cstdio>
  7. #include <vector>
  8. #include <cmath>
  9. #include <ctime>
  10. #include <list>
  11. #include <set>
  12. #include <map>
  13. using namespace std;
  14. typedef long long LL;
  15. typedef pair <int, int> P;
  16. struct trie {
  17. int cnt;
  18. trie *next[];
  19. trie() {
  20. cnt = ;
  21. for(int i = ; i < ; ++i)
  22. next[i] = NULL;
  23. }
  24. };
  25.  
  26. void insert(int num[], trie *root) {
  27. trie *p = root;
  28. for(int i = ; i >= ; --i) {
  29. if(p->next[num[i]] == NULL) {
  30. p->next[num[i]] = new trie();
  31. }
  32. p = p->next[num[i]];
  33. p->cnt++;
  34. }
  35. }
  36.  
  37. void del(int num[], trie *root) {
  38. trie *p = root;
  39. for(int i = ; i >= ; --i) {
  40. p = p->next[num[i]];
  41. p->cnt--;
  42. }
  43. }
  44.  
  45. int search(int num[], trie *root, int val) {
  46. int res = ;
  47. trie *p = root;
  48. for(int i = ; i >= ; --i) {
  49. if(p->next[num[i]^] != NULL && p->next[num[i]^]->cnt > ) {
  50. p = p->next[num[i]^];
  51. res += ( << i);
  52. }
  53. else if(p->next[num[i]] != NULL && p->next[num[i]]->cnt > ) {
  54. p = p->next[num[i]];
  55. }
  56. else {
  57. res = ;
  58. break;
  59. }
  60. }
  61. return max(val, res);
  62. }
  63.  
  64. void destory(trie *root) {
  65. if(root->next[] != NULL)
  66. destory(root->next[]);
  67. if(root->next[] != NULL)
  68. destory(root->next[]);
  69. delete(root);
  70. }
  71.  
  72. int main()
  73. {
  74. trie *root = new trie();
  75. int n, val, num[];
  76. char op[];
  77. scanf("%d", &n);
  78. while(n--) {
  79. scanf("%s %d", op, &val);
  80. int f = , temp = val;
  81. do {
  82. num[f++] = val % ;
  83. val /= ;
  84. }while(val);
  85. while(f < ) {
  86. num[f++] = ;
  87. }
  88. if(op[] == '+') {
  89. insert(num, root);
  90. }
  91. else if(op[] == '-') {
  92. del(num, root);
  93. }
  94. else {
  95. printf("%d\n", root->next[] != NULL || root->next[] != NULL ? search(num, root, temp) : temp);
  96. }
  97. }
  98. destory(root);
  99. return ;
  100. }

Codeforces 706 D. Vasiliy's Multiset (字典树贪心)的更多相关文章

  1. Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  2. CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)

    题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...

  3. codeforces 706D D. Vasiliy's Multiset(trie树)

    题目链接: D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input ...

  4. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset trie树

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  5. Codeforces 455B A Lot of Games(字典树+博弈)

    题目连接: Codeforces 455B A Lot of Games 题目大意:给定n.表示字符串集合. 给定k,表示进行了k次游戏,然后是n个字符串.每局開始.字符串为空串,然后两人轮流在末尾追 ...

  6. Educational Codeforces Round 12 E. Beautiful Subarrays 字典树

    E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...

  7. 【codeforces 514C】Watto and Mechanism(字典树做法)

    [题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...

  8. codeforces 1285D. Dr. Evil Underscores(字典树)

    链接:https://codeforces.com/problemset/problem/1285/D 题意:给n个数a1,a2,a3.....an,找到一个数X,使得X 异或所有的ai ,得到的ma ...

  9. Codeforces 633 C Spy Syndrome 2 字典树

    题意:还是比较好理解 分析:把每个单词反转,建字典树,然后暴力匹配加密串 注:然后我就是特别不理解,上面那种能过,而且时间很短,但是我想反之亦然啊 我一开始写的是,把加密串进行反转,然后单词正着建字典 ...

随机推荐

  1. 51nod1403 有趣的堆栈

    看成括号序列的话第二种方法其实就是左括号和右括号之间有多少对完整的括号. #include<cstdio> #include<cstring> #include<ccty ...

  2. Hadoop实战课程

    Hadoop生态系统配置Hadoop运行环境Hadoop系统架构HDFS分布式文件系统MapReduce分布式计算(MapReduce项目实战)使用脚本语言Pig(Pig项目实战)数据仓库工具Hive ...

  3. phpcms v9 搬家

    1.修改/caches/configs/system.php里面所有和域名有关的,把以前的老域名修改为新域名. 2.进入后台设置--站点管理,对相应的站点的域名修改为新域名. 3.点击后台右上角的更新 ...

  4. textfield tips

    关于autoSize和align属性比较好的解释,摘录下. autoSize deals with expanding the bounds of the TextField to ensure al ...

  5. 虚拟机安装centos 6 报错Erro processing drive

    错误提示: Error processing drive: pci-0000:00:10-scsi-0:0:0:0 20480MB VMware,VMware Virtual S This devic ...

  6. 《Unix网络编程》卷2 读书笔记 第1章-简介

    1. 概述 2. 进程.线程与信息共享 Unix进程间的信息共享有多种方式:注意下图中内核的位置   左边的两个进程共享存留于文件系统中某个文件上的某些信息.为访问这些信息,每个进程都得穿越内核. 中 ...

  7. AsyncTask类

    1.定义         异步任务类,在类中实现异步操作,并提供回调方法反馈当前异步执行的程度,最后反馈 的结果提供给UI主线程.         <1>Android线程         ...

  8. Linux makefile教程之函数七[转]

    使用函数 ———— 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函数的返回值可以当做 ...

  9. Unicode和汉字编码小知识

    Unicode和汉字编码小知识 将汉字进行UNICODE编码,如:“王”编码后就成了“\王”,UNICODE字符以\u开始,后面有4个数字或者字母,所有字符都是16进制的数字,每两位表示的256以内的 ...

  10. [Everyday Mathematics]20150223

    是否存在 $3\times 3$ 阶实方阵 $A$ 使得 $\tr A=0$ 且 $A^2+A^T=I$?