首页 > 代码库 > POJ 2503 Babelfish

POJ 2503 Babelfish

Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 32169 Accepted: 13832

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 ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay

Sample Output

catehloops

Hint

Huge input and output,scanf and printf are recommended.

Source

Waterloo local 2001.09.22
 
 
方法很多
快排加二分
hash表
还有这个模板Trie
 1 #include <stdio.h> 2 #include <cstring> 3  4 struct Tire 5 { 6     int next[26];//儿子节点 7     char eng[11];//单词 8 }tt[200005]; 9 10 char en[11],fr[11];11 int tp = 0;//定义在main函数外会默认为012 13 void insert(char *x,int site)14 {15     if(*x!=\n)//如果不到结尾16     {17         if(tt[site].next[*x-a]==0)//增加子节点18         {19             tt[site].next[*x-a] = ++tp;20         }21         insert(x+1,tt[site].next[*x-a]);//继续22     }else//抵达结尾23     {24         strcpy(tt[site].eng,en);25     }26 }27 28 void search(char *x,int site)29 {30     if(*x!=\0)31     {32         if(tt[site].next[*x-a]==0)//找不到!!!33         {34             printf("eh\n");35             return;36         }37         search(x+1,tt[site].next[*x-a]);//继续38     }else//找到了39     {40         printf("%s\n",tt[site].eng);41     }42 }43 44 int main()45 {46     char str[100];47     while(gets(str)&&str[0])//从stdin流中读取字符串,直至接受到换行符或EOF时停止,48     //并将读取的结果存放在buffer指针所指向的字符数组中。49     //换行符不作为读取串的内容,读取的换行符被转换为‘\0’空字符,并由此来结束字符串。50     {51         sscanf(str,"%s%s",en,fr);// 从一个字符串中读进与指定格式相符的数据。52         insert(fr,0);53     }54     while(scanf("%s",fr) != EOF){55         search(fr,0);56     }57     return 0;58 }