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;
}