【HASH】 POJ - 2503 D - Babelfish

D - Babelfish  POJ - 2503

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.

主要是输入很恶心

给你a,b两个单词,b对应a

之后给你b,让你输出a,如果没有就是eh

因为输入的限制不能直接用map<string,string>

只能用hash的方法把a存下来,把b变成一个hash值map[b]=cas

然后查询的时候算出hash值,找到map中对应的值,也就是第几个,输出答案即可

这里的base用31,因为输入的都是小写字母,所以用31,13就错了

#include <iostream>
#include <map>
#include <cstdio>
#include <cstring>
#include <string.h>
using namespace std;
const int maxn=1e5+7;
const int base=31;
map <int,int> mp;
char q[25];
char s[maxn][25];
char str[105];
int tot[maxn];
int main()
{
    int cas=0;
    while(1)
    {
        gets(str);
        int len=strlen(str);
        if(str[0]==NULL) break;
        cas++;
        int num1=0,num2=0;
        int flag=0;
        for(int i=0;i<len;i++)
        {
            if(str[i]==' ') {flag=1;continue;}
            if(!flag)
            {
                s[cas][++num1]=str[i];
                //cout<<s[cas][num1];
            }
            else
            {
                num2=num2*base+str[i];
                //cout<<str[i]<<" ";
            }
        }

        //cout<<endl;
        mp[num2]=cas;
        tot[cas]=num1;
    }

    while(~scanf("%s",q))
    {
        int len=strlen(q);
        int num=0;
        for(int i=0;i<len;i++)
        {
            num=num*base+q[i];
            //cout<<q[i]<<" ";
        }
        //cout<<endl;
        int k=mp[num];
        for(int i=1;i<=tot[k];i++)
        {
            cout<<s[k][i];
        }
        if(!mp[num]) cout<<"eh";
        cout<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值