hash+map 维护集合
来源:互联网 发布:网络测试仪哪个牌子好 编辑:程序博客网 时间:2024/05/25 18:10
Description
维护一个字符串集合:初始为空,依次处理一些插入操作,并在插入 之后输出该字符串在集合中出现的次数。
Input
输入文件包含若干行,每行为一个字符串,依次代表一个待插入的字 符串。该字符串一定非空,且仅包含英文字母和数字。
Output
对于每个插入操作输出一行一个整数,代表插入该字符串之后,该字 符串在集合中出现的次数。
Sample Input
str1
str2
str1
str1
str2
str3
Sample Output
1
1
2
3
2
1
HINT
字符串的长度不超过 100,字符串个数不超过 100000。
脸太黑,此题hash莫名超时,用C++如果会map会非常简单,虽然坑人的数据要用双hash,在直接用pair存到map里即可。
#include <cstdio>#include <iostream>#include <cstring>#include <map>long long M1=313571787;long long M2=1000000037;std::map<std::pair<long long,long long>,int>map;char st[1000];int main(){while(scanf("%s",st+1)!=EOF){int len=strlen(st+1);long long t1=0;long long t2=0;for(int i=1;i<=len;i++){(t1=t1*29+st[i]*41)%=M1;(t2=t2*31+st[i]*43)%=M2;}map[std::make_pair(t1,t2)]++;printf("%d\n",map[std::make_pair(t1,t2)]);}return 0;}
阅读全文
1 1
- hash+map 维护集合
- 维护集合
- hash map
- Hash Map / Hash Set
- 集合(hash)
- java 集合 list 接口 集合的迭代 超级for循环 扩容 链表 map Hash
- Codeforces662C【字符串hash维护+DP】
- 基础代码-维护集合
- 维护集合Ⅱ【Splay】
- hash map的使用
- c++中的hash map
- poj1840 map + hash
- C++ hash map
- hdu 1004 hash map
- C++中的hash Map
- poj 2503 【hash】【map】
- hash-map详解
- Map与Hash
- js执行环境与作用域链--总结
- Win7(64-bit)系统下的【Python3.6.0+opencv】安装配置
- HDU 5890 Eighty seven ACM/ICPC 2015 Shenyang Online(DP+bitset优化)
- 使footer固定在浏览器底部
- 在c++学习过程中要感谢的人
- hash+map 维护集合
- java类中为什么设置set和get方法
- 嵌入式Linux入门11:编程规范
- 【已解决】SpringMVC表单提交结果页面出现400错误的可能原因分析
- poj 3176 Cow Bowling
- codeforces 851 B. Arpa and an exam about geometry
- 浏览器打开一个网站可能经历哪些步骤
- Elasticsearch之—— 5.X head插件安装实例
- com.android.dx.cf.iface.ParseException