题目链接

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的更多相关文章

  1. 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 ...

  2. Codeforces 714C. Sonya and Queries Tire树

    C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...

  3. codeforces 713A A. Sonya and Queries(状态压缩)

    题目链接: A. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input st ...

  4. Codeforces Round #371 (Div. 2) C. Sonya and Queries 水题

    C. Sonya and Queries 题目连接: http://codeforces.com/contest/714/problem/C Description Today Sonya learn ...

  5. 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 ...

  6. Codeforces 713A. Sonya and Queries

    题目链接:http://codeforces.com/problemset/problem/713/A 题意: Sonya 有一个可放置重复元素的集合 multiset, 初始状态为空, 现给予三种类 ...

  7. Codeforces Round #371 (Div. 2) C. Sonya and Queries

    题目链接 分析:01trie树,很容易就看出来了,也没什么好说的.WA了一发是因为没有看见如果数字位数大于01序列的时候01序列也要补全0.我没有晚上爬起来打,白天发现过的人极多. /******** ...

  8. C. Sonya and Queries

    http://codeforces.com/contest/714/problem/C 看到这题目,想想,肯定不能暴力啊,如果用map,如何快速找到满足要求的数目,然后,长度18,我想,这不是熟悉的t ...

  9. CF #371 (Div. 2) C、map标记

    1.CF #371 (Div. 2)   C. Sonya and Queries  map应用,也可用trie 2.总结:一开始直接用数组遍历,果断T了一发 题意:t个数,奇变1,偶变0,然后与问的 ...

随机推荐

  1. MiniDao_1.6.4 版本发布,轻量级Java持久化框架,Hibernate项目辅助利器

    MiniDao 简介及特征 MiniDao 是一款超轻量的JAVA持久层框架,具备Mybatis一样的SQL能力: 支持SQL分离.支持标签.支持注解.MiniDao产生的初衷是为了解决Hiberna ...

  2. Git 上传文件到 码云 gitee

    1:git bash 执行如下 git config –global user.name “eason” git config –global user.email “your email@qq.co ...

  3. 清除linux服务器缓存 clean.sh

    #!/bin/sh#根据输入参数创建后台进程的日志名称#FileName: createNohupPhpForbak.sh #export JAVA_HOME=/root/lib/jdk1.7.0_7 ...

  4. opencv-3.3安装记录-ubuntu 14.04

    这个二逼问题不会是最后一次. ipcv-****.tar.gz 这个文件在cmake的时候会卡住,这里先下载这个文件,大概38M,放到.cache/ippcv目录下就可以了.貌似还需要改下名字. 就可 ...

  5. ubuntu oracle 环境搭建

    安装 Oracle SQL Developer Oracle客户端安装 https://oracle.github.io/odpi/doc/installation.html#linux

  6. vue:在router里面给页面加title

    vue中给组件页面加页面标题:{ path: '/', name: 'index', component: disconnect, meta: { title: '首页' } }, { path: ' ...

  7. [linux]如何更新Ubuntu的数据源

    为何要更新数据源?国外的数据源,除了速度慢这个次要因素,更可怕的是有些链接根本不通,导致用户既没有下载东西,也没有看到实质性提示:潜在的危险就是编译错误不能定位,严重时甚至重装系统.本文介绍几个国内有 ...

  8. oracle归档日志关闭和打开

    查询归档日志状态 方法一 SQL> archive log list; 方法二 SQL> select name,log_mode from V$database; 打开归档日志 orac ...

  9. 遍历DOM树,获取子节点

    获取子节点的方法有:  方法  说明  children()  选取子节点,可以带过滤参数.但只能选择子节点,不能选择孙子节点.  find()  选取子节点,可以带过滤参数.可以选择子节点及孙子节点 ...

  10. C语言中插入汇编nop指令

    工作过程中,有的时候需要打桩cycle,想在C语言中插入nop指令,可以采取的方法是 头文件中加入#inlude <stdio.h> 定义一个内联函数,然后调用这个函数,不过得测一下平台调 ...