POJ2503 Babelfish(AC 字典树)

来源:互联网 发布:linux 查看服务器配置 编辑:程序博客网 时间:2024/06/06 18:11
Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 44750 Accepted: 18893
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

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <malloc.h>#define MAXL 15#define ZIMU 26char word[MAXL];char diction[MAXL];char inputword[MAXL];char ans[MAXL];typedef struct node{char word[MAXL]; //如果是代表一个单子,这里面放的就是单子int isword;node* next[ZIMU];//这个是表示链表}nodes;nodes* head;int strlength(char *s){int size = 0;while ('\0' != *s){s++;size += 1;}return size;}void maketree(){//开始建立字典树int wordlen = strlength(word);int diclen  = strlength(diction);int i = 0;int j = 0;int index = 0;node* p = head;for (i = 0; i < diclen;i++){index = diction[i] - 'a';//从head开始找if (NULL == p->next[index]){//之前没有这个字母,那就要创建p->next[index] = (nodes*)malloc(sizeof(nodes));p->next[index]->isword = 0;for (j = 0; j < MAXL; j++){p->next[index]->word[j] = '\0';}for (j = 0; j < ZIMU; j++){p->next[index]->next[j] = NULL;}}p = p->next[index]; //p就是下一个点if (i == (diclen - 1)) //最后一个字母了,那就要把代表的单子记下来{p->isword = 1;for (j = 0; j < wordlen;j++){p->word[j] = word[j];}}}return;}void findtree(){int i = 0;node* p = head;int index = 0;int wordlen = strlength(inputword);//开始查单词,一个一个开始查for (i = 0; i < wordlen;i++){index = inputword[i] - 'a';if (NULL ==  p->next[index]){sscanf("eh","%s", ans);return;}else{if (i != (wordlen - 1)){p = p->next[index];}}if (i == (wordlen - 1)){if (1 != p->next[index]->isword){sscanf("eh", "%s", ans);//没有单词匹配return;}else{sscanf(p->next[index]->word, "%s", ans);return;}}}}int main(){char str[50];int i = 0;int j = 0;freopen("input.txt","r",stdin);//while ((scanf("%s %s", &word, &diction)) && ('\0' == word[0]))//先把所有的字符串读进来,然后判断是否是空行/*gets(s)函数与scanf("%s",s)相似,但不完全相同,使用scanf("%s",s) 函数输入字符串时存在一个问题,  就是如果输入了空格会认为字符串结束,空格后的字符将作为下一个输入项处理,但gets()函数将接收输入的整个  字符串直到遇到换行为止*///字典树根节点没有字母的head = (nodes*)malloc(sizeof(nodes));head->isword = 0;for (j = 0; j < MAXL; j++){head->word[j] = '\0';}for (j = 0; j < ZIMU; j++){head->next[j] = NULL;}while (gets(str)){if ('\0' == str[0]){break;}sscanf(str, "%s%s", &word, &diction);maketree();}//while ((2 == scanf("%s %s", &word, &diction)) && ('\0' != word[0])) //不能这么输入,这么输入空行之久就跳过了while ((1 == scanf("%s", &inputword)) && ('\0' != inputword[0])) //这个后面没有空行,可以这么写{findtree();printf("%s\n", ans);}return 0;}


 
原创粉丝点击