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 字典树
题意:还是比较好理解 分析:把每个单词反转,建字典树,然后暴力匹配加密串 注:然后我就是特别不理解,上面那种能过,而且时间很短,但是我想反之亦然啊 我一开始写的是,把加密串进行反转,然后单词正着建字典 ...
随机推荐
- 在view中常见的四种方法的使用场合
四种方法,使view创建好里面就有东西:[1.init 2.initWithFrame使用代码创建的时候.(从文件创建的时候不一定调用:1.init 2.initWithFrame这两个方法) 3 ...
- POJ 2594 Treasure Exploration (可相交最小路径覆盖)
题意 给你张无环有向图,问至少多少条路径能够覆盖该图的所有顶点--并且,这些路径可以有交叉. 思路 不是裸的最小路径覆盖,正常的最小路径覆盖中两个人走的路径不能有重复的点,而本题可以重复. 当然我们仍 ...
- Python [Leetcode 350]Intersection of Two Arrays II
题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...
- [asp.net] 通过JS实现对treeview控件的复选框单选控制。
前端JS代码: //识别不同的浏览器 function getTargetElement(evt) { var elem if (evt.target) { elem = (evt.target.no ...
- Android WebView中那些不得不解决的坑~~
前面那张hybrid开发心得 有人问 怎么解决不用onJsPrompt 来回调js函数的问题.其实很简单,就是在在你的jscalljava回调函数内 另外开个线程去load js代码即可: wb.po ...
- Kyoto Cabinet(DBM) + Kyoto Tycoon(网络层)
项目原地址kyotocabinet: http://fallabs.com/kyotocabinet/ kyototycoon: http://fallabs.com/kyototyc ...
- android定时三种方式
一.采用Handler与线程的sleep(long)方法二.采用Handler的postDelayed(Runnable, long)方法三.采用Handler与timer及TimerTask结合的方 ...
- 嵌入式 VFS: Cannot open root device "mtdblock2" or unknown-block(2,0)
系统启动后,虽然nand驱动表现正常,但是最后挂载rootfs时候出错: Kernel command line: root=/dev/mtdblock2 rw init=/linuxrc conso ...
- Jquery 设置style:display 通过ID隐藏区域
$("#id").css('display','none'); $("#id").css('display','block'); 或 $("#id&q ...
- Java IO读写大文件的几种方式及测试
读取文件大小:1.45G 第一种,OldIO: public static void oldIOReadFile() throws IOException{ BufferedReader br = n ...