zoukankan      html  css  js  c++  java
  • 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] == '')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
    ", s1[Hash[h]]);
    40         else printf("eh
    ");
    41     }
    42 
    43     return 0;
    44 }
  • 相关阅读:
    博客园创业点子摘录
    DNN性能优化方案系列(2)Page State Persistence
    以页面模块(模块定义)为添加单位的DNN控制窗格(ControlPanel)
    在SQL Server中安全的创建,使用,和删除一个临时表
    IDataErrorInfo and Business rule validation
    转:通过避免下列 10 个常见 ASP.NET 缺陷使网站平稳运行
    iis6启动HTTP压缩的方法
    TcpSocket编程与Event编写学习的好例子
    转:IIS aspnet HTTP 压缩 与Ajax
    070508_设置上次考试_考生管理_成绩批量录入
  • 原文地址:https://www.cnblogs.com/Penn000/p/5839899.html
Copyright © 2011-2022 走看看