Babelfish(二分查找,字符串的处理略有难度,用sscanf输入)

Babelfish
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 28581   Accepted: 12326

题目链接:http://poj.org/problem?id=2503

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.

Source

代码:
 
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<iostream>
 5 using namespace std;
 6 struct vode
 7 {
 8     char eng[30];
 9     char dic[30];
10 };
11 struct vode g[100005];
12 int strr ( const void *a , const void *b )
13 {
14      return strcmp( ((vode *)a)->dic , ((vode *)b)->dic );
15 }
16 int binsearch(int s,int t,char dic[])
17 {
18     int low=s,high=t,mid;
19     if(s<=t)
20     {
21         mid=low+(high-low)/2;
22         if(strcmp(g[mid].dic,dic)==0)return mid;
23         if(strcmp(g[mid].dic,dic)>0)
24         return binsearch(low,mid-1,dic);
25         else
26         return binsearch(mid+1,high,dic);
27     }
28     return -1;
29 }
30 int main()
31 {
32     char dic[30];
33     int i;
34     //下面的for循环控制字典的输入和结束,是本题的重点所在******
35     for(i=0;dic[0]!='\0';i++)
36     {
37         gets(dic);
38         sscanf(dic,"%s%s",g[i].eng,g[i].dic);
39         //printf("dic[%d]:%s g[%d].eng:%s g[%d].dic:%s\n\n",i,dic,i,g[i].eng,i,g[i].dic);//验证输出
40     }
41     qsort(g,i,sizeof(g[0]),strr);
42     while(scanf("%s",dic)!=EOF)
43     {
44         int flag=binsearch(0,i-1,dic);
45         if(flag>=0)cout<<g[flag].eng<<endl;
46         else cout<<"eh"<<endl;
47     }
48     return 0;
49 }
View Code

 

posted @ 2013-08-20 12:59  狂盗一枝梅  阅读(615)  评论(0编辑  收藏  举报