翻译方言.1030

哈希查询挺好用的

输入格式

输入最多由100000个字典条目组成,后面是一个空行,后面是一个最多100000个词语的消息。
空行前面的若干行,每个字典条目是一行,包含普通话词语、后面跟着一个空格、空格后面是一个方言。
每一个方言都有唯一确定的普通话与之对应,也就是说在字典里没有任何一个方言出现不止一次。
空行后面的若干行,表示你需要查询的词语。
每一行有一个词语。
题目中说的词语,都是最长10个小写字母的序列。

输出格式

输出方言对应的普通话词语。
如果方言在词典没有找到,你应该说“eh”。

样例

input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

output

cat
eh
loops

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

const int maxn=100000;

struct dot
{
    string dialect="";
    string speech="";
    dot *next =NULL;
};

struct HASH
{
    dot* head=NULL;
    dot* tail=NULL;
};

HASH d[maxn];

int main()
{
    string input;
    while(getline(cin,input))
    {
        if(input.length()==0)
            break;

        string s,temp;
        int i=0;
        while(input[i]!=' ')
        {
            temp+=input[i++];
        }
        int len=input.length();
        i++;
        int hash_num=1;
        while(i<len)
        {
            hash_num*=input[i];
            hash_num%=maxn;
            s+=input[i++];
        }

        if(d[hash_num].head==NULL)
        {
            d[hash_num].head=d[hash_num].tail=new dot;
            d[hash_num].tail->dialect=s;
            d[hash_num].tail->speech=temp;
        }
        else
        {
            d[hash_num].tail->next=new dot;
            d[hash_num].tail->next->dialect=s;
            d[hash_num].tail->next->speech=temp;
            d[hash_num].tail=d[hash_num].tail->next;
        }
    }
    string diction;
    while(getline(cin,diction))
    {
        if(diction.length()==0)
            break;
        int flag=1;
        int hash_num=1;
        int len=diction.length();
        for(int i=0;i<len;i++)
        {
            hash_num*=diction[i];
            hash_num%=maxn;
        }
        if(d[hash_num].head!=NULL)
        {
            dot* temp=d[hash_num].head;
            while(temp->next!=NULL)
            {
                if(temp->dialect==diction)
                {
                    flag=0;
                    cout<<temp->speech<<endl;
                    goto FLAG;
                }
                temp=temp->next;
            }
            if(temp->dialect==diction && flag==1)
            {
                flag=0;
                cout<<temp->speech<<endl;
            }
        }
        FLAG:
        if(flag)
            cout<<"eh"<<endl;

    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值