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

题意很简单不多说。

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

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

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
struct trie {
int cnt;
trie *next[];
trie() {
cnt = ;
for(int i = ; i < ; ++i)
next[i] = NULL;
}
}; void insert(int num[], trie *root) {
trie *p = root;
for(int i = ; i >= ; --i) {
if(p->next[num[i]] == NULL) {
p->next[num[i]] = new trie();
}
p = p->next[num[i]];
p->cnt++;
}
} void del(int num[], trie *root) {
trie *p = root;
for(int i = ; i >= ; --i) {
p = p->next[num[i]];
p->cnt--;
}
} int search(int num[], trie *root, int val) {
int res = ;
trie *p = root;
for(int i = ; i >= ; --i) {
if(p->next[num[i]^] != NULL && p->next[num[i]^]->cnt > ) {
p = p->next[num[i]^];
res += ( << i);
}
else if(p->next[num[i]] != NULL && p->next[num[i]]->cnt > ) {
p = p->next[num[i]];
}
else {
res = ;
break;
}
}
return max(val, res);
} void destory(trie *root) {
if(root->next[] != NULL)
destory(root->next[]);
if(root->next[] != NULL)
destory(root->next[]);
delete(root);
} int main()
{
trie *root = new trie();
int n, val, num[];
char op[];
scanf("%d", &n);
while(n--) {
scanf("%s %d", op, &val);
int f = , temp = val;
do {
num[f++] = val % ;
val /= ;
}while(val);
while(f < ) {
num[f++] = ;
}
if(op[] == '+') {
insert(num, root);
}
else if(op[] == '-') {
del(num, root);
}
else {
printf("%d\n", root->next[] != NULL || root->next[] != NULL ? search(num, root, temp) : temp);
}
}
destory(root);
return ;
}

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. IOS中设置cell的背景view和选中时的背景view 、设置cell最右边的指示器(比如箭头\文本标签)

    一.Cell的设置 1.设置cell的背景view和选中时的背景view UIImageView *bg = [[UIImageView alloc] init]; bg.image = [UIIma ...

  2. Android隐藏虚拟按键,关闭开机动画、开机声音

    /*********************************************************************** * Android隐藏虚拟按键,关闭开机动画.开机声音 ...

  3. HDU 5360 Hiking 登山 (优先队列,排序)

    题意: 有n个人可供邀请去hiking,但是他们很有个性,每个人都有个预期的人数上下限[Li,Ri],只有当前确定会去的人数在这个区间内他才肯去.一旦他答应了,无论人数怎样变更,他都不会反悔.问最多能 ...

  4. ORACLE clusterware组成

    oracle cluterware是一个单独的安装包,一旦安装部署好后,每个节点上的oracle cluterware会自动启动,oracle cluterware的运行环境由两个磁盘文件,若干后台进 ...

  5. Windows下PHP+Eclipse开发环境搭建 及错误解决(apache2.2服务无法启动 发生服务特定错误:1)

    前言 Eclipse与php/apache的关系:Eclipse只是用来写代码的,如果想要在浏览器查看运行效果就要让php/apache的运行目录指向你的代码目录.Eclipse貌似不会自己和apac ...

  6. Nodepad++ tab改成4个空格

    设置-首选项-选项卡设置-使用空格替换

  7. C# Datatable的Select方法

    lubiaopan 原文 Datatable的Select()方法简介 DataTable是我们在进行开发时经常用到的一个类,并且经常需要对DataTable中的数据进行筛选等操作,下面就介绍一下Da ...

  8. ASP.NET 如何做出简单的验证码

    如果说要做验证码,那不得不提的就是GDI+绘图了.我们都知道验证码是以图片形式展示的,而且是动态生成的,这样就需要我们去画出它. 科普一下,什么是GDI+? GDI+是图形设备接口(GDI)的高级版本 ...

  9. MySQL_PHP学习笔记_2015_0614_PHP传参总结_URL传参_表单传参

    1. PHP 传参总结   1.1 url 传参     解析方法(下面两种解读方式均可以): $firstName1 = $_GET['firstName']; $firstName2 = $_RE ...

  10. C语言实现strcmp

    注意转化为unsigned char: strcmp.h #ifndef STRCMP_H #define STRCMP_H /************************************ ...