在线翻译
原创
©著作权归作者所有:来自51CTO博客作者不想悲伤到天明的原创作品,请联系作者获取转载授权,否则将追究法律责任
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".
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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std ;
const int MAX = 100005 ;
typedef struct dic{
char e1[20] ;
char e2[20] ;
}Dic;
int cmp(Dic a ,Dic b)
{
// 从小到大排序
return strcmp(a.e2,b.e2)<0 ;
}
void Binary_search(Dic *s, int left ,int right ,char *key )
{
int i ;
int mid ;
int flag = 0 ;
while (left <=right )
{
mid = left + (right - left )/2 ;
if (strcmp(key,s[mid].e2)==0 )
{
printf("%s\n",s[mid].e1);
flag = 1 ;
break ;
}
else if(strcmp (key ,s[mid].e2 )>0)
{
left = mid + 1 ;
}
else
{
right = mid -1 ;
}
}
if(flag == 0)
{
printf("eh\n");
}
return ;
}
int main()
{
Dic s[MAX] ;
int i ;
int n ;
char word[20] ;
int num = 0 ;
while(true)
{
scanf("%s%s",s[num].e1 ,s[num].e2);
num++ ;
cin.get();
if(cin.peek() == '\n')
break ;
}
n = num ;
sort(s,s+n ,cmp);
while(scanf("%s",word)!=EOF)
{
Binary_search(s,0,num-1,word);
}
/*
测试数据 :
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
*/
return 0 ;
}