Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 28103 | Accepted: 12128 |
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
题意:
输入一系列单词 输出对应单词
代码:
#include<iostream>
#include<string>
#include<map>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#define MAX 100001
using namespace std;
typedef struct
{
char tran[20];
char str[20];
}node;
node s[MAX];
int i=0;
int cmp(const void *a,const void *b)
{
node *c=(node *)a,*d=(node *)b;
return strcmp(c->str,d->str);
}
int bin_search(char *t)
{
int l=0,r=i-1;
int mid;
while(l<=r)
{
mid=(l+r)/2;
if(strcmp(s[mid].str,t)==0) return mid;
else if(strcmp(s[mid].str,t)<0) l=mid+1;
else r=mid-1;
}
return -1;
}
int main()
{
char tmp[20];
while(true)
{
char t;
if((t=getchar())=='\n') break;
s[i].tran[0]=t;
int j=1;
while(true)
{
t=getchar();
if(t==' ') { s[i].tran[j++]='\0'; break; }
else s[i].tran[j++]=t;
}
scanf("%s",s[i].str);
getchar();
i++;
}
qsort(s,i,sizeof(node),cmp);
while(gets(tmp)!=NULL)
{
int j=bin_search(tmp);
if(j==-1) printf("eh\n");
else printf("%s\n",s[j].tran);
}
return 0;
}
思路:
二分查找法
处理空行的时候比较麻烦