STL之map应用 +hash表(51nod 1095)
2016-04-05 15:36
357 查看
题目:Anigram单词
题意:给出词典,再给出一些单词,求单词的Anigram数量。
思路:先将字串转换成哈希表,然后再用map链接。
hash表构造方法汇总:http://www.cnblogs.com/gj-Acit/archive/2013/05/06/3062628.html
此题使用除留余数法。
View Code
题意:给出词典,再给出一些单词,求单词的Anigram数量。
思路:先将字串转换成哈希表,然后再用map链接。
hash表构造方法汇总:http://www.cnblogs.com/gj-Acit/archive/2013/05/06/3062628.html
此题使用除留余数法。
#include <iostream> #include <algorithm> #include <stdlib.h> #include <time.h> #include <math.h> #include <stdio.h> #include <string> #include <string.h> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #define c_false ios_base::sync_with_stdio(false); cin.tie(0) #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3f #define zero_(x,y) memset(x , y , sizeof(x)) #define zero(x) memset(x , 0 , sizeof(x)) #define MAX(x) memset(x , 0x3f ,sizeof(x)) #define swa(x,y) {LL s;s=x;x=y;y=s;} using namespace std; #define N 10005 const int MOD = 1e7+7; const int base = 57; const double PI = acos(-1.0); typedef long long LL ; map <string, int> MAP; int has[MOD+1], cnt[base+1]; char s[11]; int n,m; int Hash(){ zero(cnt); for(int i = 0; s[i] ;i++){ if(s[i] >= 'a' && s[i] <= 'z') cnt[s[i] - 'a']++; else cnt[s[i] - 'A' +26]++; } int res = 0; for(int i = 0; i < base; i++){ res = (res*base + cnt[i])%MOD; } return res; } int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); //ios_base::sync_with_stdio(false); cin.tie(0); scanf("%d", &n); while(n--){ scanf("%s", s); string k = s; ++has[Hash()]; if(MAP[k] ==0) MAP[k]++; } scanf("%d", &m); while(m--){ scanf("%s", s); string k = s; int num = Hash(); int sum = has[num]; if(MAP[k]>0) sum--; ///记得把自己减去; printf("%d\n",sum); } return 0; }
View Code
相关文章推荐
- HTML常用布局
- JBPM4 常用表结构及其说明
- sort(水题)
- svn检出项目后,serverlet包 报错
- 在Eclipse中使用JUnit4进行单元测试(初级篇)
- 2016年4月4日作业
- IDEA导入eclipse项目
- wifidog 代码简单分析(2)
- LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
- 插入排序
- hdu 1030 Delta-wave
- 10g静默安装以及静默升级
- Docker使用常见问题
- Nodejs in Visual Studio Code 02.学习Nodejs
- C++使用json的方法,把include放到项目目录下
- 妙味课堂——HTML+CSS(第四课)(一)
- [LeetCode]题解(python):135-Candy
- springmvc url-pattern配置/*报错
- 死锁产生的原因及四个必要条件(2017腾讯实习生题目)
- JQuery-01