Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 33188 | Accepted: 14269 |
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
这个题目目前知道的方法可以有三种,一种是快排+二分,一种是hash,一种是MAP
注意的是:在输入的时候,字典和要查询的词是使用空行隔开的,这里用到sscanf()这个函数,关于这个函数的详细介绍,参见上一篇博客
链接地址为:http://blog.csdn.net/codeball/article/details/39858145
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
char name1[20],name2[20];
};
node trie[100009];
int cmp(node a,node b)
{
return strcmp(a.name2,b.name2)<0;
}
int main()
{
char xs[50];
int i,j,k = 0;
while(gets(xs))
{
if(xs[0] == '\0')
break;
sscanf(xs,"%s%s",trie[k].name1,trie[k].name2);
k++;
}
sort(trie,trie+k,cmp);
int bj = 0,mid;
while(gets(xs))
{
i = 0;
j = k;
bj = 0;
while(i <= j)
{
mid = (i + j) >> 1;
if(strcmp(trie[mid].name2,xs) > 0)
{
j = mid - 1;
}
else if(strcmp(trie[mid].name2,xs) < 0)
{
i = mid+1;
}
else
{
printf("%s\n",trie[mid].name1);
bj = 1;
break;
}
}
if(bj == 0)
{
printf("eh\n");
}
}
return 0;
}