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 字典树
题意:还是比较好理解 分析:把每个单词反转,建字典树,然后暴力匹配加密串 注:然后我就是特别不理解,上面那种能过,而且时间很短,但是我想反之亦然啊 我一开始写的是,把加密串进行反转,然后单词正着建字典 ...
随机推荐
- 51nod1403 有趣的堆栈
看成括号序列的话第二种方法其实就是左括号和右括号之间有多少对完整的括号. #include<cstdio> #include<cstring> #include<ccty ...
- Hadoop实战课程
Hadoop生态系统配置Hadoop运行环境Hadoop系统架构HDFS分布式文件系统MapReduce分布式计算(MapReduce项目实战)使用脚本语言Pig(Pig项目实战)数据仓库工具Hive ...
- phpcms v9 搬家
1.修改/caches/configs/system.php里面所有和域名有关的,把以前的老域名修改为新域名. 2.进入后台设置--站点管理,对相应的站点的域名修改为新域名. 3.点击后台右上角的更新 ...
- textfield tips
关于autoSize和align属性比较好的解释,摘录下. autoSize deals with expanding the bounds of the TextField to ensure al ...
- 虚拟机安装centos 6 报错Erro processing drive
错误提示: Error processing drive: pci-0000:00:10-scsi-0:0:0:0 20480MB VMware,VMware Virtual S This devic ...
- 《Unix网络编程》卷2 读书笔记 第1章-简介
1. 概述 2. 进程.线程与信息共享 Unix进程间的信息共享有多种方式:注意下图中内核的位置 左边的两个进程共享存留于文件系统中某个文件上的某些信息.为访问这些信息,每个进程都得穿越内核. 中 ...
- AsyncTask类
1.定义 异步任务类,在类中实现异步操作,并提供回调方法反馈当前异步执行的程度,最后反馈 的结果提供给UI主线程. <1>Android线程 ...
- Linux makefile教程之函数七[转]
使用函数 ———— 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函数的返回值可以当做 ...
- Unicode和汉字编码小知识
Unicode和汉字编码小知识 将汉字进行UNICODE编码,如:“王”编码后就成了“\王”,UNICODE字符以\u开始,后面有4个数字或者字母,所有字符都是16进制的数字,每两位表示的256以内的 ...
- [Everyday Mathematics]20150223
是否存在 $3\times 3$ 阶实方阵 $A$ 使得 $\tr A=0$ 且 $A^2+A^T=I$?