POJ2503 Babelfish map或者hash_map
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的更多相关文章
- POJ2503——Babelfish(map映射+string字符串)
Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...
- map,hash_map, hash_table, 红黑树 的原理和使用
在刷算法题的时候总是碰到好多题,号称可以用hash table来解题.然后就蒙圈了. 1.首先,map和hash_map的区别和使用: (1)map底层用红黑树实现,hash_map底层用hash_t ...
- Map和hash_map
map和hash_map 今天在写拼流的程序时碰到一个问题,要根据流的四元组的结构信息映射到该流的数据.也就是我在网络数据包拼接的过程中,要根据包的地址和端口信息,对应到其对应的一个流的数据上去,把端 ...
- STL中map与hash_map容器的选择收藏
这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...
- map与hash_map使用与对比
#include <iostream> #include <functional> #include <map> #include <ext/hash_map ...
- STL 中的map 与 hash_map的理解
可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...
- STL中的map和hash_map
以下全部copy于:http://blog.chinaunix.net/uid-26548237-id-3800125.html 在网上看到有关STL中hash_map的文章,以及一些其他关于STL ...
- map、hash_map、unordered_map 的思考
#include <map> map<string,int> dict; map是基于红黑树实现的,可以快速查找一个元素是否存在,是关系型容器,能够表达两个数据之间的映射关系. ...
- map vs hash_map
1. map, multimap, set, multiset g++ 中 map, multimap, set, multiset 由红黑树实现 map: bits/stl_map.h multim ...
随机推荐
- 树莓派 -- 输入设备驱动 (key) 续1
测试 安装 input-utils pi@raspberrypi:~ $ sudo apt-get install input-utils Reading package lists... Done ...
- Buffer.from(arrayBuffer[, byteOffset[, length]])
Buffer.from(arrayBuffer[, byteOffset[, length]]) arrayBuffer - 一个 TypedArray 或 new ArrayBuffer() 的 . ...
- Python之函数作业
Python之函数作业 爬页面 #爬虫页面,send一次爬一次 from urllib.request import urlopen def get(): while True: url = yiel ...
- 【面试题】LRU算法及编码实现LRU策略缓存
概念 LRU(least recently used)就是将最近不被访问的数据给淘汰掉,LRU基于一种假设:认为最近使用过的数据将来被使用的概率也大,最近没有被访问的数据将来被使用的概率比较低. 原理 ...
- uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)
题意: 有5种操作: PUSH:加入“{}”空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出 ...
- 远程调试nodejs
一 windows作为远程服务器 1.在远程服务器(192.168.1.1)上安装node-inspector:npm install -g node-inspector // -g 导入安装路径 ...
- mariadb,maria db
mariadb,maria db 继续紧逼Oracle:在占领谷歌等公司之后,MariaDB迈向企业端 发表于2013-10-25 13:00|10618次阅读|10条评论 MariaDB在Googl ...
- msp430入门编程03
msp430的C标识符和关键字 msp430入门学习 msp430入门编程
- Linux下汇编语言学习笔记41 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- CentOS7下搭建postfix邮箱服务器并实现extmail的web访问
http://blog.51cto.com/zero01/2064693 https://blog.csdn.net/a5nan/article/details/52510887