[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,然后与问的 ...
随机推荐
- linux系统基础入门
使用工具:源码管理,自动部署,web服务器(linux) linux系统基础入门 1.下载地址 2.本文介绍的是一个基于Ubuntu的开源操作系统 下载优麒麟 Ubuntu是一个广泛应用于个人电脑,云 ...
- English-旅游英语及情景对话
1.旅游英语:预订机票情景对话及常用句型 目前,越来越多的人都选择以飞机为出行方式.但是如何用一口流利的英语订机票呢?这里我们替你总结了一些情景对话,还有一些常用的句型.大家都来学一学吧~A:Good ...
- Python 百分号字符串拼接
# %s可以接收一切 %d只能接收数字 msg = 'i am %s my hobby is %s' %('lhf','alex') print msg msg2 = 'i am %s my hobb ...
- IP地址的含义
不管是学习网络还是上网,IP地址都是出现频率非常高的词.Windows系统中设置IP地址的界面如图1所示,图中出现了IP地址.子网掩码.默认网关和DNS服务器这几个需要设置的地方,只有正确设置,网络才 ...
- CentOS 6.x 默认源中带了mysql-server,可以使用yum安装。
1.执行安装命令:yum install mysql-server 2.初始化数据库,使用命令1# service mysqld start 启动MSQL service mysqld stop ...
- 11.mysql-权限.md
目录 -- ***********五.mysql权限问题**************** -- mysql数据库权限问题:root :拥有所有权限(可以干任何事情) -- 权限账户,只拥有部分权限(C ...
- proxool连接sqlerver
原先proxool连接sqlserver,用的是sqljdbc,不知道怎么回事,怎么也连接不上.下面的代码既不报错也不执行下去,应该是驱动出了问题,网上也很难找到sqljdbc什么版本. if (_c ...
- C++ 关于滚动条的滚动问题
SCROLLINFO scrollinfo; GetScrollInfo(SB_HORZ, &scrollinfo, SIF_ALL); switch (nSBCode) { case SB_ ...
- EF 踩过的坑
ef + mysql-8.0.12-winx64 这个版本的mysql,当一个类为树型结构,会迁移报错. 数据迁移提示:No connection string named 'TaoBaoEntiti ...
- idea gradle卡主问题
http://services.gradle.org/distributions/ 首先下载 all 版本 ,解压 ,再d盘, 并保留zip 文件 ,新建环境变量 GRADLE_HOME 指向 ...