Codeforces 706 D. Vasiliy's Multiset (字典树贪心)
题目链接: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 (字典树贪心)的更多相关文章
- 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 ...
- CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)
题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...
- codeforces 706D D. Vasiliy's Multiset(trie树)
题目链接: D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input ...
- 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 ...
- Codeforces 455B A Lot of Games(字典树+博弈)
题目连接: Codeforces 455B A Lot of Games 题目大意:给定n.表示字符串集合. 给定k,表示进行了k次游戏,然后是n个字符串.每局開始.字符串为空串,然后两人轮流在末尾追 ...
- Educational Codeforces Round 12 E. Beautiful Subarrays 字典树
E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...
- 【codeforces 514C】Watto and Mechanism(字典树做法)
[题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...
- codeforces 1285D. Dr. Evil Underscores(字典树)
链接:https://codeforces.com/problemset/problem/1285/D 题意:给n个数a1,a2,a3.....an,找到一个数X,使得X 异或所有的ai ,得到的ma ...
- Codeforces 633 C Spy Syndrome 2 字典树
题意:还是比较好理解 分析:把每个单词反转,建字典树,然后暴力匹配加密串 注:然后我就是特别不理解,上面那种能过,而且时间很短,但是我想反之亦然啊 我一开始写的是,把加密串进行反转,然后单词正着建字典 ...
随机推荐
- HDU 3068 (Manacher) 最长回文
求一个字符串的最长子串,Manacher算法是一种O(n)的算法,很给力! s2[0] = '$',是避免在循环中对数组越界的检查. 老大的代码: http://www.cnblogs.com/Big ...
- 堆Heap
#pragma once#include <vector> // 小堆template<class T> //仿函数struct Less{ bool opera ...
- Jave 鼠标点击画太极 PaintTaiji (整理)
package demo; /** * Jave 鼠标点击画太极 PaintTaiji (整理) * 声明: * 又是一份没有注释的代码,而且时间已经久远了,不过代码很短,解读起来应该 * 不会很麻烦 ...
- Sublime 的中文乱码问题
Sublime Text 是现在最受欢迎的文本编辑器,没有之一.它非常简洁,而且对各种代码的高亮显示很美观.但是,它默认不支持 GBK.Shift-JIS 等中文.日本编码格式,故打开此类文件会出现乱 ...
- 用 Xcode 开发 Cydia Substrate 插件(二)
上次介绍了一个如何用 Xcode 来构建 Substrate 插件,但是开发的具体过程还没有涉及,而这往往又正是初学者最难下手的地方,所以有了本文的后续. 不过在开始之前你要先做好思想准备,相比较开发 ...
- Python内置数据类型之Dictionary篇
1.查看函数XXX的doc string. Python的函数是有属性的,doc string便是函数的属性.所以查看函数XXX的属性的方法是模块名.XXX.__doc__ 2.模块的属性 每个模块都 ...
- POJ2236 Wireless Network
解题思路:简单并查集,注意时间限制是10000MS,每次进行O操作之后, 进行一次for循环,进行相关调整.同时注意输入输出格式,见代码: #include<cstdio> #incl ...
- SpringMVC注解@initbinder解决类型转换问题
在使用SpringMVC的时候,经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,而SpringMVC默认不支持这个格式的转换,所以需要手动配置,自定义数据的绑定才能解决这个问题.在 ...
- adb shell 查找并删除文件
# -*- coding: cp936 -*- ## function: remove file ## remark: python version -- import os,sys import l ...
- SQLServer如何快速生成100万条不重复的随机8位数字
最近在论坛看到有人问,如何快速生成100万不重复的8位编号,对于这个问题,有几点是需要注意的: 1. 如何生成8位随机数,生成的数越随机,重复的可能性当然越小 2. 控制不重复 3. ...