终于AC了!这个题目仍然是字典树的应用,不过需要注意许多细节问题。
1:当出现公共前缀时 如 火星文是 wert 原文是 asdf ,如果我们输入 we ,这时输出的应该还是we
2:千万注意处理关键字合并出现的情况,如果合并出现,则在字典树中视为不存在!
3:笔者认为此题目最关键的是字典树结构的定义与初始化,具体见代码
What Are You Talking About
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 102400/204800K (Java/Other)
Total Submission(s) : 20 Accepted Submission(s) : 12
Problem Description
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
Output
In this problem, you have to output the translation of the history book.
Sample Input
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
Sample Output
hello, i'm from mars. i like earth!简单的翻译器#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> typedef struct Node{ int n; char t[11]; struct Node *next[26]; }Trie,*BiTrie; char str[3300]; void Init_Trie(BiTrie &T){ //初始化字典树 int i; T=(BiTrie)malloc(sizeof(Trie)); for(i=0;i<26;i++) { T->next[i]=NULL; } }//Init_Trie void Creat_Trie(BiTrie &T,char *b,char *c){ Trie *q,*p; q=T; int len=strlen(c),i,j; for(i=0;i<len;i++) { if(q->next[c[i]-'a']==NULL){ p=(BiTrie)malloc(sizeof(Trie)); q->next[c[i]-'a']=p; q=q->next[c[i]-'a']; q->n=0;//初始化为零 for(j=0;j<26;j++) { q->next[j]=NULL; }//for }//if else{ q=q->next[c[i]-'a']; }//else }//for q->n=1;//记录单词的最后一个位置 strcpy(q->t,b); }//Creat_Trie void Search(BiTrie T,char *a){ Trie *q; q=T; int i,j,n=strlen(a); for(i=0;i<n;i++) { if(q->next[a[i]-'a']!=NULL) { q=q->next[a[i]-'a']; }//if else break; }//for if(q->n==1&&i==n)printf("%s",q->t);//判断条件非常关键!!!!! else{ printf("%s",a); }//else } int main() { char a[11],b[11],c[11]; int i,len,j; BiTrie T; Init_Trie(T);//初始化字典树 while(scanf("%s",a)){ if(strcmp(a,"START")==0) continue; else if(strcmp(a,"END")==0) break; else{ strcpy(b,a); scanf("%s",c); Creat_Trie(T,b,c); }//else }//while getchar(); while(gets(str)){ if(strcmp(str,"START")==0) continue; else if(strcmp(str,"END")==0) break; else{ len=strlen(str); j=0,i=0; for(i=0;i<len;i++) { if(isalpha(str[i])){ j=0; while(isalpha(str[i])) { a[j++]=str[i++]; } a[j]='\0'; Search(T,a); } if(i==len);//易错点 else printf("%c",str[i]); } puts(""); }//else }//while // system("pause"); return 0; }