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 }
  • 相关阅读:
    kafka系列教程5(客户端实践)
    log4j示例-Daily方式(log4j.properties)
    Log4j.xml配置(rolling示例)
    Log4j配置详解之log4j.xml
    kafka 常用命令
    linux下进程cpu占用过高问题定位方法
    Linux 操作系统主机名变成bogon怎么解决?
    网络基础 Windows控制台下Ftp使用简介
    网络基础 cookie详解
    网络基础 http 会话(session)详解
  • 原文地址:https://www.cnblogs.com/Penn000/p/5839899.html
Copyright © 2011-2022 走看看