题目: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+;
const int base = ; const double PI = acos(-1.0);
typedef long long LL ; map <string, int> MAP;
int has[MOD+], cnt[base+];
char s[];
int n,m;
int Hash(){
zero(cnt);
for(int i = ; s[i] ;i++){
if(s[i] >= 'a' && s[i] <= 'z') cnt[s[i] - 'a']++;
else cnt[s[i] - 'A' +]++;
}
int res = ;
for(int i = ; 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] ==) MAP[k]++;
}
scanf("%d", &m);
while(m--){
scanf("%s", s);
string k = s;
int num = Hash();
int sum = has[num];
if(MAP[k]>) sum--; ///记得把自己减去;
printf("%d\n",sum);
}
return ;
}

STL之map应用 +hash表(51nod 1095)的更多相关文章

  1. C++ STL hash表用法

    C++ STL unordered_map用法 在C++11中,unordered_map作为一种关联容器,替代了hash_map,unordered_map的底层实现是hash表,所以被称为无序关联 ...

  2. 深入了解STL中set与hash_set,hash表基础

    一,set和hash_set简介 在STL中,set是以红黑树(RB-Tree)作为底层数据结构的,hash_set是以哈希表(Hash table)作为底层数据结构的.set可以在时间复杂度为O(l ...

  3. STL中map与hash_map的比较

    1. map : C++的STL中map是使用树来做查找算法; 时间复杂度:O(log2N) 2. hash_map : 使用hash表来排列配对,hash表是使用关键字来计算表位置; 时间复杂度:O ...

  4. STL中map与hash_map容器的选择收藏

    这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...

  5. 计蒜客 成绩统计 (Hash表)

    链接 : Here! 思路 : 如果用 $STL$ 的 $map$ 或者是使用 $unordered\underline{}map$ 的话是会 $T$ 的, 所以得手写一个 $hash表$. 其实这个 ...

  6. 第9章:LeetCode--算法:HASH表

    哈希表(Hash table,也叫散列表),关键值K和内容的映射表,通过映射函数实现,hashtable(key,value) 进行查询的时候,就是使用哈希函数将关键码key转换为对应的数组下标,并定 ...

  7. STL之map与pair与unordered_map常用函数详解

    STL之map与pair与unordered_map常用函数详解 一.map的概述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称 ...

  8. 使用C++STL的map容器实现一种命令映射

    因为最近在练习写一个ftp的服务器,其中的命令有很多种,每个命令对应一个执行函数,能够想到的最简单的实现方式便是使用if--else匹配命令和执行对应的函数,如下所示: if(strcmp(" ...

  9. PHP数组/Hash表的实现/操作、PHP变量内核实现、PHP常量内核实现 - [ PHP内核学习 ]

    catalogue . PHP Hash表 . PHP数组定义 . PHP变量实现 . PHP常量实现 1. PHP Hash表 0x1: 基本概念 哈希表在实践中使用的非常广泛,例如编译器通常会维护 ...

随机推荐

  1. linux 安装软件程序

    1.用aptitude管理软件包 查看已安装的/未安装的等软件包 无法通过aptitude看到一个细节是所有跟某个特定软件包关联的所有文件的列表.利用dpkg命令能看到这个列表. dpkg -L pa ...

  2. unity3d c#脚本定义Transform

    Transform n = Instantiate(newobject,transform.position,transform.rotation) as Transform;Instantiate返 ...

  3. (LinkedList) Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  4. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  5. TCP/IP 之大明王朝邮差

    本系列文章全部摘选自"码农翻身"公众号,仅供个人学习和分享之用.文章会给出原文的链接地址,希望不会涉及到版权问题. 个人感言:真正的知识是深入浅出的,码农翻身" 公共号将 ...

  6. hive查看建表语句

    查看hive建表语句:show create table tablename; 查看hive表结构:describe  tablename; 简写:desc tablename;

  7. 内省(Introspector)

    内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法 目的:主要用于传递数据信息,这种类中的方法主要用于访问私有的字段(且方法名符合某种命名规则) p ...

  8. centos7.x/RedHat7.x重命名网卡名称

    从51CTO博客迁移出来几篇博文. 在CentOS7.x或RedHat7.x上,网卡命名规则变成了默认,既自动基于固件.拓扑结构和位置信息来确定.这样一来虽然有好处,但也会影响操作,因为新的命名规则比 ...

  9. zk源码环境搭建

    zk不是使用maven管理的. 将zk的src下的代码导入eclipse,lib下的jar包导入工程. QuorumPeerMain类的main方法是入口,启动了zk的server,参数是conf文件 ...

  10. Appium学习路-打包apk和ipa篇

    间隔这么长时间再去写Appium的学习篇是有原因的,因为在想要用appium测试ios时,发现appium只能测试debug版本的ipa包.然后就需要自己去学习打包了啊.然后就对xcode各种不了解, ...