您的位置:首页 > 其它

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

此题使用除留余数法。

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: