POJ2503(hash)

Babelfish

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 41263   Accepted: 17561

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

Hint

Huge input and output,scanf and printf are recommended.

思路:把字符串哈希成26进制数,然后查找的复杂度就是线性的。字符串共有26^10种可能,约等于10^14,可以用long long存下。使用map,key为哈希值,value为该串的位置。

 1 //2016.9.4
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <map>
 7 #define N 100005
 8
 9 using namespace std;
10
11 char s1[N][12], s2[N][12], str[25];
12 map<long long, int> Hash;
13
14 long long F(char* s)//把字符串hash成一个26进制数
15 {
16     int len = strlen(s);
17     long long h = 0;
18     for(int i = 0; i < len; i++)
19         h = h*26+(s[i]-‘a‘);
20     return h;
21 }
22
23 int main()
24 {
25     int cnt = 0;
26     while(gets(str))
27     {
28         if(str[0] == ‘\0‘)break;
29         sscanf(str,"%s%s", s1[cnt], s2[cnt]);
30         long long h = F(s2[cnt]);
31         Hash[h] = cnt;
32         cnt++;
33     }
34     while(gets(str))
35     {
36         if(str[0] == ‘\0‘)break;
37         long long h = F(str);
38         if(Hash.find(h)!=Hash.end())//map按key查找,失败返回end
39         printf("%s\n", s1[Hash[h]]);
40         else printf("eh\n");
41     }
42
43     return 0;
44 }
时间: 2024-08-14 16:32:57

POJ2503(hash)的相关文章

poj2503 Babelfish BKDRhash+链式hash

题目链接 题意:给定字符串以及对应的字符串,再给字符串找到对应的字符串,不存在输出"eh". 思路:造模板. /********************************************************* file name: poj2503.cpp author : kereo create time: 2015年04月12日 星期日 17时13分12秒 ******************************************************

POJ-2503 Babelfish---map或者hash

题目链接: https://vjudge.net/problem/POJ-2503 题目大意: 就像查找一本字典,根据输入的条目和要查询的单词,给出查询结果(每个单词长度不超过10) 解题思路: map容器可以直接过,不过为了练习hash,写了个hash也可以过 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<map>

一致性hash算法 - consistent hashing

1.背景 我们都知道memcached服务器是不提供分布式功能的,memcached的分布式完全是由客户端来实现的.在部署memcached服务器集群时,我们需要把缓存请求尽可能分散到不同的缓存服务器中,这样可以使得所有的缓存空间都得到利用,而且可以降低单独一台缓存服务器的压力.     最简单的一种实现是,缓存请求时通过计算key的哈希值,取模后映射到不同的memcahed服务器.这种简单的实现在不考虑集群机器动态变化的情况下也是比较有效的一种方案,但是,在分布式集群系统中,简单取模的哈希算法

BZOJ3198 SDOI2013 spring HASH+容斥原理

题意:给定6个长度为n的数列,求有多少个数对(i,j)((i,j)≡(j,i))使得i和j位置恰好有K个数相同,其中0≤K≤6 题解: 设fi=至少有K个数相同的位置对的数量,用2^6枚举每一种可能,根据容斥原理,答案就是\[\sum\limits_{i = K}^N {{f_i}C_i^K{{\left( { - 1} \right)}^{i - K}}} \] 至于多乘一个组合数,是因为当前枚举到有x个数相同,一个位置对有i个相同的数,那么累计的时候就会算成$C_x^i$,因此实际上这个位置

hash算法搜索获得api函数地址的实现

我们一般要获得一个函数的地址,通常采用的是明文,例如定义一个api函数字符串"MessageBoxA",然后在GetProcAddress函数中一个字节一个字节进行比较.这样弊端很多,例如如果我们定义一个杀毒软件比较敏感的api函数字符串,那么可能就会增加杀毒软件对我们的程序的判定值,而且定义这些字符串还有一个弊端是占用的字节数较大.我们想想如何我们的api函数字符串通过算法将它定义成一个4字节的值,然后在GetProcAddress中把AddressOfNames表中的每个地址指向的

BZOJ_3207_花神的嘲讽计划1_(Hash+主席树)

描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3207 给出一个长度为\(n\)的串,以及\(m\)个长度为\(k\)的串,求每个长度为\(k\)的串在原串\([x,y]\)区间是否出现过. 分析 这道题要求对比长度为\(k\)的串,于是我们把这些串的Hash值都算出来,问题就转化成了求\([x,y]\)的区间中是否出现过某Hash值. 求区间中某一个值出现了多少次,可以用主席树. p.s. 1.学习了主席树指针的写法,比数组慢好多啊...

Hash算法专题

1.[HDU 3068]最长回文 题意:求一个字符串(len<=110000)的最长回文串 解题思路:一般解法是manacher,但是这一题用hash也是可以ac的 假设当前判断的是以i为中心偶数最长回文串,那么s[2*i+1-k……i]与s[i+1……k]的哈希值必定相同 假设当前判断的是以i为中心奇数最长回文串,那么s[2*i-k……i-1]与s[i+1……k]的哈希值必定相同 用二分求出相应的k 1 #include <iostream> 2 #include <algori

Salted hash password

参考文档 http://www.cnblogs.com/richardlee/articles/2511321.html https://en.wikipedia.org/wiki/Salt_%28cryptography%29 https://www.91ri.org/7593.html 密码存储为什么不能是明文? 当账户密码是明文存储的话, 万一本网站给黑客攻破获取了数据, 则用户的账户被泄露.(术语叫 拖库) 当黑客知道了你的账户后, 其可以使用此账户,到其他网站尝试访问, 例如有厉害关系

Golang Hash MD4

//Go标准包中只有MD5的实现 //还好,github上有MD4实现. package main import (     "golang.org/x/crypto/md4"     "encoding/hex"     "fmt" ) func get_md4(buf []byte) ([] byte) { ctx := md4.New() ctx.Write(buf) return ctx.Sum(nil) } func main() {