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

  1. dog ogday
  2. cat atcay
  3. pig igpay
  4. froot ootfray
  5. loops oopslay
  6.  
  7. atcay
  8. ittenkay
  9. oopslay

Sample Output

  1. cat
  2. eh
  3. 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][], s2[N][], str[];
  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 = ;
  18. for(int i = ; i < len; i++)
  19. h = h*+(s[i]-'a');
  20. return h;
  21. }
  22.  
  23. int main()
  24. {
  25. int cnt = ;
  26. while(gets(str))
  27. {
  28. if(str[] == '\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')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 ;
  44. }

POJ2503(hash)的更多相关文章

  1. POJ-2503 Babelfish---map或者hash

    题目链接: https://vjudge.net/problem/POJ-2503 题目大意: 就像查找一本字典,根据输入的条目和要查询的单词,给出查询结果(每个单词长度不超过10) 解题思路: ma ...

  2. POJ 2503 Babelfish(map,字典树,快排+二分,hash)

    题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...

  3. 复杂的 Hash 函数组合有意义吗?

    很久以前看到一篇文章,讲某个大网站储存用户口令时,会经过十分复杂的处理.怎么个复杂记不得了,大概就是先 Hash,结果加上一些特殊字符再 Hash,结果再加上些字符.再倒序.再怎么怎么的.再 Hash ...

  4. 对抗密码破解 —— Web 前端慢 Hash

    (更新:https://www.cnblogs.com/index-html/p/frontend_kdf.html ) 0x00 前言 天下武功,唯快不破.但在密码学中则不同.算法越快,越容易破. ...

  5. 散列表(hash table)——算法导论(13)

    1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...

  6. hash表长度优化证明

    hash表冲突的解决方法一般有两个方向: 一个是倾向于空间换时间,使用向量加链表可以最大程度的在节省空间的前提下解决冲突. 另外一个倾向于时间换空间,下面是关于这种思路的一种合适表长度的证明过程: 这 ...

  7. SQL Server-聚焦查询计划Stream Aggregate VS Hash Match Aggregate(二十)

    前言 之前系列中在查询计划中一直出现Stream Aggregate,当时也只是做了基本了解,对于查询计划中出现的操作,我们都需要去详细研究下,只有这样才能对查询计划执行的每一步操作都了如指掌,所以才 ...

  8. C# salt+hash 加密

    一.先明确几个基本概念 1.伪随机数:pseudo-random number generators ,简称为:PRNGs,是计算机利用一定的算法来产生的.伪随机数并不是假随机 数,这里的" ...

  9. SQL 提示介绍 hash/merge/concat union

    查询提示一直是个很有争议的东西,因为他影响了sql server 自己选择执行计划.很多人在问是否应该使用查询提示的时候一般会被告知慎用或不要使用...但是个人认为善用提示在不修改语句的条件下,是常用 ...

随机推荐

  1. [iOS Animation]-CALayer 显示方式

    寄宿图 图片胜过千言万语,界面抵得上千图片 ——Ben Shneiderman 我们在第一章『图层树』中介绍了CALayer类并创建了一个简单的有蓝色背景的图层.背景颜色还好啦,但是如果它仅仅是展现了 ...

  2. Delphi中unicode转汉字函数(转)

    源:Delphi中unicode转汉字函数 近期用到这个函数,无奈没有找到 delphi 自带的,网上找了下 有类似的,没有现成的,我需要的是 支持 “\u4f00 ” 这种格式的,即前面带标准的 “ ...

  3. php学习-数组(一)

    数组函数可以对大量性质相同的数据进行存储,排序,插入及删除等操作. 学习任务: 声明数组,输出数组,遍历数组,查询数组中指定元素,获取数组中的最后一个元素. 删除数组中重复的元素.统计数组中元素的个数 ...

  4. webservice-概念性学习(一)

    以下是本人原创,如若转载和使用请注明转载地址.本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!博客地址 学习webservice之前呢,我想说我们先学习以下的知识,对你以后的学习 ...

  5. Android实时监听网络状态

    Android实时监听网络状态(1)   其实手机在网络方面的的监听也比较重要,有时候我们必须实时监控这个程序的实时网络状态,android在网络断开与连接的时候都会发出广播,我们通过接收系统的广播就 ...

  6. javascript基础教程学习总结(1)

    摘自javascript基础教程 开始: 1.将脚本放在哪里: 1.1 放在html和<html>之间 范例: <!DOCTYPE html PUBLIC "-//W3C/ ...

  7. ajax常用写法

    $.ajax( url,{ type:"post", data:{},}) .done(function() { alert("success"); }) .f ...

  8. IOS开发中深拷贝与浅拷贝

    简而言之: 1.对不可变的非集合对象,copy是指针拷贝,mutablecopy是内容拷贝 2.对于可变的非集合对象,copy,mutablecopy都是内容拷贝 3.对不可变的数组.字典.集合等集合 ...

  9. listview 去掉header和footer中的分割线

    在listView中加上android:headerDividersEnabled="false" android:footerDividersEnabled="fals ...

  10. OutputStream()

    ---恢复内容开始--- OutputStream 类是一个专门进行字节数据输出的一个类.