Babelfish - 散列表

Babelfish

TimeLimit:3000MS  MemoryLimit:65536K
64-bit integer IO format: %lld
已解决 |  点击收藏
Problem 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".
SampleInput
 
   
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
SampleOutput
 
   
cat
eh
loops

做法,hash

#include<cstdio>
#include<cstring>
#define M 1000000
struct node
{
    char a[11];
    char b[11];
} Hash[M];

int ELFhash(char *key)
{
    unsigned long h=0;
    while(*key)
    {
        h=(h<<4)+(*key++);
        unsigned long g=h&0Xf0000000L;
        if(g)h^=g>>24;
        h&=~g;
    }
    return h%M;
}

int main()
{
    char str[40],ttr[11],ltr[11];
    int key;
    for(int i=0; i<M; i++)
    {
        Hash[i].a[0]='\n';
    }
    while (gets(str)&&strlen(str))
    {
        sscanf(str,"%s%s",ttr,ltr);
        key = ELFhash(ltr);
        while(Hash[key].a[0]!='\n')
            key=(key+1)%M;
        strcpy(Hash[key].a,ltr);
        strcpy(Hash[key].b,ttr);
    }
    while(gets(ttr)&&strlen(ttr))
    {
        key=ELFhash(ttr);
        while(Hash[key].a[0]!='\n'&&strcmp(ttr,Hash[key].a)!=0)
        {
            key=(key+1)%M;
        }
        if(Hash[key].a[0]=='\n')
            puts("eh");
        else
            puts(Hash[key].b);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37804064/article/details/79721363