047:Babelfish
DescriptionYou 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. 宝贝鱼翻译机 (Bablefish)
题目描述
你刚刚从滑铁卢(Waterloo)搬到了一个新的大城市。这里的人们都说着一种令人费解的外语方言。幸运的是,你有一本可以帮助你理解的词典。
输入
输入包含最多100,000条词典条目,接下来有一个空行,其次是最多100,000词的原始信息。
每一个词典条目包含用空格分开的两个字符串S1和S2,其中S1是译文,S2是原文。原始信息为多行,一行一个需要翻译的单词。
输入数据保证每一个单词的程长度不超过10个字符。
输出
对于每一个需要翻译的单词,输出翻译后的单词。若原始单词不存在在词典中,输出"eh"(不包含引号)。 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
char s1,s2;
}a;
int cmp(node a,node b)
{
return strcmp(a.s2,b.s2)<0;
}
int len;
int main()
{
len=0;
char str;
while(gets(str))
{
if(str=='\0')
break;
sscanf(str,"%s%s",&a.s1,&a.s2);
len++;
}
sort(a,a+len,cmp);
while(gets(str))
{
int l=0,r=len-1,mid,flag=1;
while(l<=r)
{
mid=(l+r)>>1;
int ans=strcmp(str,a.s2);
if(ans==0)
{
printf("%s\n",a.s1);
flag=0;
break;
}
else if(ans>0)
l=mid+1;
else
r=mid-1;
}
if(flag)
printf("eh\n");
}
return 0;
}
页:
[1]