F - Babelfish解题报告

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.
函数名: bsearch   功 能: 二分法搜索   用 法: void *bsearch(const void *key, const void *base, size_t nelem, size_t width, int(*fcmp)(const void *, const *));   
语法:    #include <stdlib.h> void *bsearch( const void *key, const void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );  
参数:     第一个:要查找的关键字。第二个:要查找的数组。第三个:指定数组中元素的数目。第四个:每个元素的长度(以字符为单位)。第五个:指向比较函数的指针。   
功能:      函数用折半查找法在从数组元素buf[0]到buf[num-1] 匹配参数key。如果函数compare 的第一个参数小于第二个参数,返回负值;如果等于返回零值;如果大于返回正值。
             数组buf 中的元素应以升序排列。函数bsearch()的返回值是指向匹配项,如果没有发现匹配项,返回NULL
 原型:extern int strcmp(const char *s1,const char * s2);   
用法:#include <string.h>   
功能:比较字符串s1和s2。   一般形式:strcmp(字符串1,字符串2)   
说明:   当s1<s2时,返回值<0   当s1=s2时,返回值=0   
           当s1>s2时,返回值>0   
     即:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止。如:  
      "A"<"B" "a">"A" "computer">"compare"   特别注意:strcmp(const char *s1,const char * s2)这里面只能比较字符串,不能比较数字等其他形式的参数。This source is shared by qiuge

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct abc
{
	char eng[11];
	char fh[11];
};
struct  abc a[1000001];

int fcmp(const void *p0,const void *p1)  //bsearch.
{
    return  strcmp(((abc*)p0)->fh,((abc*)p1)->fh);
}
int cmp(const void *p3,const void *p4)   //qsort.
{
	return  strcmp((char*)p3,((abc*)p4)->fh);
}
int main()
{
	int sum=0,sign=0,i,j,len,k;
	char s[30];
	struct abc *p;
	sum=0;
	while(gets(s))
	{
		
		len=strlen(s);
		if(len==0)
			break;
		else
		{
			for(i=0;i<len;i++)
			{
				if(s[i]==' ')
					break;
		    	a[sum].eng[i]=s[i];
				
			}
			a[sum].eng[i]='\0';
			for(k=0,j=i+1;j<len;j++,k++)
			{
			
				a[sum].fh[k]=s[j];
			}
			a[sum].fh[k]='\0';
			sum++;
		}

	}
	qsort(a,sum,sizeof(abc),fcmp);
	while(gets(s))
	{
		
       p=(abc*)bsearch(s,a,sum,sizeof(abc),cmp);
	   if(p)
		   puts(p->eng);
	   else
		   puts("eh");
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值