关闭

poj-2503 Babelfish

122人阅读 评论(0) 收藏 举报
                                                          F - Babelfish
Crawling in process...Crawling failedTime Limit:3000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

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
先定义一个结构体用来储存字典,然后对字典中的英文进行排序,输入单词,进行查找,若找到,输出相对应的英文,若找不到则就输出eh.
#include<stdio.h>  
#include<string.h>  
#include<stdlib.h>  
struct diction
{  
    char eng[11];
    char fn[11];
};  
struct diction a[1000001];   
  
int fcmp(const void * a,const void *b)
{  
    return strcmp(((diction*)a)->fn, ((diction*)b)->fn);  
}  
  
int cmp(const void* a, const void* b) 
{  
    return strcmp((char*)a, ((diction*)b)->fn);  
}  
  
int main()  
{  
    int i,j,k,sum,sign,len;  
    char str[30];  
    struct diction *p;  
    sum=0;  
    sign=0;  
    while(gets(str))  
    {  
        len=strlen(str);  
        if(len==0)  
            break;  
        else  
        {  
            for(i=0;i<len;i++)  
            {  
                if(str[i]==' ')  
	     			break;
                a[sum].eng[i]=str[i];  
            }  
            a[sum].eng[i]='\0'; 
            for(j=i+1,k=0;j<len;j++,k++)  
		    	a[sum].fn[k]=str[j]; 
			a[sum].fn[k]='\0';  
            sum++;  
        }  
    }  
    qsort(a,sum,sizeof(diction),fcmp); 
    while(gets(str))  
    {  
        p=(diction*)bsearch(str,a,sum,sizeof(diction),cmp);
        if(p)  
            puts(p->eng);  
       else  
            puts("eh");  
    }  
   return 0; 
}  

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20977次
    • 积分:379
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:22篇
    • 译文:0篇
    • 评论:0条
    文章存档