[Codeforces_713A]Sonya and Queries
题目链接
http://codeforces.com/problemset/problem/713/A
题意
三种操作:
+ ai 集合里加一个整数ai,相同数记为多个。
- ai 集合里减一个该整数ai,若集合中存在多个则减一个,保证减操作时集合中有该数至少一个。
? s 输出集合中匹配模式s的整数有多少个,s是01串,0表示对应位为偶数,1表示对应位为奇数,若模式和整数位数不同则左侧补0补齐。For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not.
此外,整数ai不超过10^18,模式s长度不超过18位。
总操作次数n<=10^6。
思路
原思路:是每次?操作时,遍历集合中的数看是否匹配模式。
结果:在n量级大时超时。
正确思路的点:
首先,整数ai和模式s都可以映射到唯一的数pos上,左侧补0无影响。
其次,ai和s映射到数pos的用相同方法处理。
故把串各位依次取奇1偶0,然后得到的数pos结果只有2^18种,开一个数组存每种结果的出现次数即可,?操作时直接输出数组对应pos的值即可,而不用再遍历当时的集合元素计算有多少个元素匹配该模式,降低时间复杂度。
相关知识点
位操作
代码
#include <stdio.h>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;
int cnt[1<<19];
int trans(char * str){
int num=0;
for(size_t i=0;i<strlen(str);i++){
int x=str[i]-'0';
num+=x&1;
num=num<<1;
}
return num;
}
int main(int argc, const char * argv[]) {
memset(cnt,0,sizeof(cnt));
int n;
scanf("%d",&n);
while(n--){
char op[2];
char str[20];
int num;
scanf("%s%s",op,str);
num=trans(str);
if(op[0]=='+'){cnt[num]++;}
else if(op[0]=='-'){cnt[num]--;}
else{
printf("%d\n",cnt[num]);
}
}
return 0;
}
[Codeforces_713A]Sonya and Queries的更多相关文章
- Codeforces Round #371 (Div. 2) C. Sonya and Queries[Map|二进制]
C. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 714C. Sonya and Queries Tire树
C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...
- codeforces 713A A. Sonya and Queries(状态压缩)
题目链接: A. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input st ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries 水题
C. Sonya and Queries 题目连接: http://codeforces.com/contest/714/problem/C Description Today Sonya learn ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries —— 二进制压缩
题目链接:http://codeforces.com/contest/714/problem/C C. Sonya and Queries time limit per test 1 second m ...
- Codeforces 713A. Sonya and Queries
题目链接:http://codeforces.com/problemset/problem/713/A 题意: Sonya 有一个可放置重复元素的集合 multiset, 初始状态为空, 现给予三种类 ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries
题目链接 分析:01trie树,很容易就看出来了,也没什么好说的.WA了一发是因为没有看见如果数字位数大于01序列的时候01序列也要补全0.我没有晚上爬起来打,白天发现过的人极多. /******** ...
- C. Sonya and Queries
http://codeforces.com/contest/714/problem/C 看到这题目,想想,肯定不能暴力啊,如果用map,如何快速找到满足要求的数目,然后,长度18,我想,这不是熟悉的t ...
- CF #371 (Div. 2) C、map标记
1.CF #371 (Div. 2) C. Sonya and Queries map应用,也可用trie 2.总结:一开始直接用数组遍历,果断T了一发 题意:t个数,奇变1,偶变0,然后与问的 ...
随机推荐
- VSFTP 配置虚拟用户
虚拟用户的特点是只能访问服务器为其提供的FTP服务,而不能访问系统的其它资源.所以,如果想让用户对FTP服务器站内具有写权限,但又不允许访问系统其它资源,可以使用虚拟用户来提高系统的安全性. 在VSF ...
- python_03 各种运算符
1.算数运算 2.比较运算 3.赋值运算 4.逻辑运算 先计算括号中表达式 计算顺序:and,or,not在一个表达式中从前到后计算, 若and前一个元素为false则立刻返回为False,不计算后面 ...
- fb发布打包外部资源
将资源放在src文件夹下面即可 然后在打包那就会看到资源,勾上即可
- Leetcode 题解 reverse List II
这个题确实太容易错了. 我已经做了2遍了,之前都是套用reverse List 1中的函数. 现在尝试用新方法,在一个函数里完成,结果又错了. 事实证明,永远不要想当然!!!白板编程真的是要求,你对每 ...
- Windows系统封装总结
注:使用虚拟机或者实体机进行封装均可,实体机进行封装的成功率更高.虚拟机进行封装建议使用VMware,12版本.过高的版本容易造成封装失败 一. Windows 10系统封装 1 ...
- Java IO流学习总结七:Commons IO 2.5-FileUtils
在上面的几篇文章中,介绍了IO的常规用法,今天介绍 Commons IO 框架的使用. Commons IO简介 Apache Commons IO是Apache基金会创建并维护的Java函数库.它提 ...
- 关于C# WinForm中进度条的实现方法
http://www.cnblogs.com/Sue_/articles/2024932.html 进度条是一个软件人性化考虑之一,他给用户的感觉就是程序内部在不停的动作,执行到了什么程度,而不是整个 ...
- js版RSA算法
// RSA, a suite of routines for performing RSA public-key computations in// JavaScript.//// Requires ...
- thymeleaf 字面量
- C# 中文判断
原地址忘了,也是博客园的 /// <summary> /// 是否是 中文 /// </summary> /// <param name="CString&qu ...