http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109Language of FatMouse
Time Limit: 10 Seconds Memory Limit: 32768 KB
We all know that FatMouse doesn't speak English. But now he has to be prepared since our nation will join WTO soon. Thanks to Turing we have computers to help him.
Input Specification
Input consists of up to 100,005 dictionary entries, followed by a blank line, followed by a message of up to 100,005 words. Each dictionary entry is a line containing an English word, followed by a space and a FatMouse word. No FatMouse word appears more than once in the dictionary. The message is a sequence of words in the language of FatMouse, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output Specification
Output is the message translated to English, one word per line. FatMouse words not in the dictionary should be translated as "eh".
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
cat eh loops
Source: Zhejiang University Training Contest 2001
解题总结:这一题给的时间挺长的,10S,用C++里的模板map就可以通过了,好像也不超过两秒的样子,但是作为字典树的练
习,还是以字典树做了,第一次写字典树,没有参考别人的用法,就自个儿琢磨了个递归生成与遍历~实现:用结构题存储其英文
翻译及当前字符所能对应的next 指针,生成树时,对当前对应字符的指针进行判断,不存在则增加,存在直接进入递归直至新增
添的单词至末尾。查找时用同样的思路~、、、
出现的问题:结构体设置不合理,导致超出内存;内存申请时运用错误(node=(Tree)malloc(sizeof(Node)/*Tree*/)),花
了好久跟踪还能找出错误,,,末尾字符判断不精确,WA~
/*zoj 1109 100ms Auther :dooder */ #include<stdio.h> #include<string.h> #include<stdlib.h> //list [][15],这样存储超内存 /*#define 100006 typedef struct node{ int k; struct node *p[26]; }Node,*Tree; char list[][27]; char s[105]; Tree t; int k;*/ typedef struct node{ char word[11]; struct node *p[26]; }Node,*Tree; char s[22]; Tree t; Tree init() { int i; Tree t=(Tree)malloc(sizeof(Node)); for(i=0;i<26;i++) t->p[i]=NULL; memset(t->word,0,sizeof(t->word)); return t; } /*//有以下函数的修改,主要问题是末字符位置不够清晰 void addnode(Tree node,char *word,int d) {//搜索,插入新结点,末结点的k值赋为单词的下标 Tree cur; if(node->p[word[d]-'a']==NULL){ cur=init(); node->p[word[d]-'a']=cur; } if(word[d+1]) addnode(node->p[word[d]-'a'],word,d+1); else node->k=k-1; } */ //对以上函数进行了修改, void addnode(Tree node,char *word,int d) {//搜索,插入新结点,末结点的k值赋为单词的下标, Tree cur; if(!word[d]){ strcpy(node->word,s); return ; } if(node->p[word[d]-'a']==NULL){ cur=init(); node->p[word[d]-'a']=cur; } addnode(node->p[word[d]-'a'],word,d+1); } void find(Tree node,char *word,int d) {//查找,并输出结果 if(!word[d]){ printf("%s/n",node->word); return ; } if(node->p[word[d]-'a']==NULL){ printf("eh/n"); return ; } find(node->p[word[d]-'a'],word,d+1); } int main() {//zoj 1109 char *p,ith; t=init(); while(gets(s)){ if(p=strchr(s,' ')){ *p=0; addnode(t,p+1,0); } else break; } while(gets(s)) find(t,s,0); return 0; }