哈希查询挺好用的
输入格式
输入最多由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;
}