Babelfish
| Time Limit:3000MS | Memory Limit:65536K | |
| Total Submissions:17068 | Accepted:7376 |
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<stdio.h>
typedef struct _dic
{
char str1[11];
char str2[11];
};
struct _dic dic[100000];
int n=0;
int Binary_Search(char ch[])
{
int low=0,high=n-1,mid=0;
int equal;
if(!strcmp(dic[low].str2,ch)) return low;
else if(!strcmp(dic[high].str2,ch)) return high;
while(low<=high)
{
mid=low+(high-low)/2;//printf("mid=%d ",mid);
equal=strcmp(dic[mid].str2,ch);//printf("equal=%d/n",equal);
if(equal==0) return mid;
else if(equal==-1) low=mid+1;
else high=mid-1;
}
return -1;
}
int cmp( const void *a ,const void *b)
{
return strcmp((*(struct _dic *)a).str2,(*(struct _dic *)b).str2);
}
int main()
{
char cache[30];
int tmp;
int i;
freopen("input","r",stdin);
while(gets(cache)&&strcmp(cache,"")!=0)
{
sscanf(cache,"%s %s",dic[n].str1,dic[n].str2);
n++;
}
qsort(dic,n,sizeof(dic[0]),cmp);
while(scanf("%s",cache)!=EOF)
{
tmp=Binary_Search(cache);
if(tmp!=-1) printf("%s/n",dic[tmp].str1);
else printf("eh/n");
}
}
本文介绍了一个名为Babelfish的翻译挑战问题,通过构建一个简易的双向词汇对照表来实现从一种语言到另一种语言的基本翻译功能。该程序利用二分查找提高搜索效率,并通过qsort函数对字典进行排序。
1456

被折叠的 条评论
为什么被折叠?



