Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 26333 | Accepted: 11301 |
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
依然是二分搜索的思想,不过这次,因为数据全部是字符串,所以在比较过程中,用到字符串函数Strcmp,算是字符串与二分搜索的一个集中体现,一开始WA了下,一定要注意每道题检测下特殊值与边界值,比如输入为一个空字符,则依然要输出对应的“eh”。。。而且发现用scanf读字符串特别不靠谱,根本就无法检测出
输入数据之间的空行,在杨宇大神的指点下,学会了SSCANF函数。膜拜下。。。。。
#include <cstdio>
#include <cstring>
#include <algorithm>
struct diction
{
char dic[15];
char dia[15];
} data[110000];
bool cmp(diction x,diction y)
{
return (strcmp(x.dia,y.dia)<0);
}
int main()
{
char ch[15];
int t=0;
char s[30];
while (gets(s)&&s[0]!='\0')
{
sscanf(s,"%s%s",data[t].dic,data[t].dia);
t++;
}
std::sort(data,data+t,cmp);
while (gets(ch))
{
int min=0,mid,max=t;
for(;;)
{
mid=(max-min)/2+min;
if (mid==min) break;
if (strcmp(data[mid].dia,ch)<=0) min=mid;
else max=mid;
}
if (strcmp(ch,data[mid].dia)==0)
printf("%s\n",data[mid].dic);
else puts("eh");
}
return 0;
}