POJ2503 这是一道水题,用Map轻松AC。

不过,可以拿来测一下字符串散列, 毕竟,很多情况下map无法解决的映射问题需要用到字符串散列。

自己生成一个质数, 随便搞一下。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string.h>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1e+6;
const int P=999983;
unsigned int base[100];
int dic[maxn];
char eng[100010][11];
inline void init_hash(char* s,int h[])
{
int l=strlen(s);
h[0]=0;
for(int i=1;i<=l;++i)
{
h[i]=(h[i-1]*26+s[i-1])%P;
}
base[0]=0;
for(int i=1;i<=l;i++)
base[i]=(base[i-1]*26)%P;
} inline unsigned int string_hash(char* s,int h[], int l, int r)//[l,r)左闭右开
{
init_hash(s,h);
return (h[r]-h[l]*base[r-1])%P;
}
int ha[maxn];
int main()
{freopen("t.txt","r",stdin);
//ios::sync_with_stdio(false);
memset(dic,0,sizeof(dic));
char lan[11];
int ditlen=1,tot=0;
eng[0][0]='e';
eng[0][1]='h';
eng[0][3]='\0';
while(true)
{
char t=getchar();
if(t=='\n')break;
int len=1;
eng[ditlen][0]=t;
while(true)
{
t=getchar();
if(t==' '){eng[ditlen][len++]='\0';ditlen++;break;}
eng[ditlen][len++]=t;
}
int lent=0;
while(true)
{
t=getchar();
if(t=='\n'){lan[lent++]='\0';break;}
lan[lent++]=t;
}
int leng=strlen(lan);
int has=string_hash(lan,ha,0,leng);
while(has<0)has+=P;
dic[has]=ditlen-1;
}
while(scanf("%s",lan)!=EOF)
{
int has=string_hash(lan,ha,0,strlen(lan));
while(has<0)has+=P;
has=dic[has];
for(int i=0;;i++)
{
if(eng[has][i]=='\0')break;
printf("%c",eng[has][i]);
}
printf("\n");
}
return 0;
}

  

POJ2503 Babelfish map或者hash_map的更多相关文章

  1. POJ2503——Babelfish(map映射+string字符串)

    Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...

  2. map,hash_map, hash_table, 红黑树 的原理和使用

    在刷算法题的时候总是碰到好多题,号称可以用hash table来解题.然后就蒙圈了. 1.首先,map和hash_map的区别和使用: (1)map底层用红黑树实现,hash_map底层用hash_t ...

  3. Map和hash_map

    map和hash_map 今天在写拼流的程序时碰到一个问题,要根据流的四元组的结构信息映射到该流的数据.也就是我在网络数据包拼接的过程中,要根据包的地址和端口信息,对应到其对应的一个流的数据上去,把端 ...

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

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

  5. map与hash_map使用与对比

    #include <iostream> #include <functional> #include <map> #include <ext/hash_map ...

  6. STL 中的map 与 hash_map的理解

    可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...

  7. STL中的map和hash_map

    以下全部copy于:http://blog.chinaunix.net/uid-26548237-id-3800125.html 在网上看到有关STL中hash_map的文章,以及一些其他关于STL ...

  8. map、hash_map、unordered_map 的思考

    #include <map> map<string,int> dict; map是基于红黑树实现的,可以快速查找一个元素是否存在,是关系型容器,能够表达两个数据之间的映射关系. ...

  9. map vs hash_map

    1. map, multimap, set, multiset g++ 中 map, multimap, set, multiset 由红黑树实现 map: bits/stl_map.h multim ...

随机推荐

  1. Zabbix微信告警

    Zabbix微信告警 摘要 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信 ...

  2. Spring 事物注解属性

    @Transactional属性 . propagation 事物的传播属性 . isolation 事物的隔离属性 . readonly 设置只读属性 . timeout 设置超时属性 . roll ...

  3. L2-001. 紧急救援 (Dijkstra算法打印路径)

    作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...

  4. UI & 原型

    UI & 原型 产品原型设计与团队实时协作平台 https://www.xiaopiu.com/download https://www.xiaopiu.com/forum/posts/5b9 ...

  5. markman & psd

    markman & psd MarkMan 设计稿标 & 测量神器 http://www.getmarkman.com/ https://www.jianshu.com/p/83af3 ...

  6. Apple & APPID & iOS & React Native

    Apple & APPID & iOS & React Native 在没有 苹果开发者账号证书 APPID, ios 是否支持导出 app https://developer ...

  7. 前端开发:JQuery(1)

    JQuery DOM文档加载的步骤: 1. 解析HTML结构: 2. 加载外部脚本和样式: 3. 解析并执行脚本代码: 4. DOM树构建完成: 5. 加载图片等外部文件: 6. 页面加载完成: JS ...

  8. 贝尔数--Codeforces908E. New Year and Entity Enumeration

    给n<=50个长度m<=1000的二进制数,记他们为集合T,求满足下面条件的集合S数:令$M=2^m-1$,1.$a \epsilon S \Rightarrow a \ \ xor \ ...

  9. The Java library for converting Wikipedia wikitext notation to HTML

    https://code.google.com/p/gwtwiki/ The Java Wikipedia API (Bliki engine) is a parser library for con ...

  10. Codeforces 22E(图论)

    题意: 给出n个节点,以及和这个节点指向的节点fi,表示从i能够到达fi,问至少需要添加多少条边能够使得原图变为强连通分量, 输出边数及添加的边,多解输出任意一组解. 2 <= n <= ...