HDU 1075 What Are You Talking About(map或字典树)
What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 24624 Accepted Submission(s): 8280
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!
Hint
Huge input, scanf is recommended.
Author
Ignatius.L
Recommend
题意 第一个start和end之间的字符串中,表示字典,后者可以翻译成前者。
第二个start与end之间,每行一句话,进行翻译,如果该单词在字典中存在,就翻译再输出,否则直接输出,符号等也直接输出
使用字典树解决该问题的时间效率更高
map代码(1100ms)
#include <cstdio> #include <iostream> #include <cstring> #include <map> using namespace std; map<string,string>mp; char str1[15]; char str2[15]; char str[3100]; char res[3100]; int len; int main() { scanf("%s",str1); while(scanf("%s",str1)!=EOF) { if(!strcmp(str1,"END")) break; scanf("%s",str2); mp[str2]=str1; // cout<<mp[str2]<<endl; } getchar(); gets(str); while(gets(str)) { if(!strcmp(str,"END")) break; int l=0; int r=0; for(int i=0;i<strlen(str);i++) { if(str[i]>='a'&&str[i]<='z') res[l++]=str[i]; else { res[l]=0; if(mp.find(res)!=mp.end()) //如果mp中不存在该元素,mp.find()==mp.end() cout<<mp[res]; else printf("%s",res); printf("%c",str[i]); l=0; } } printf("\n"); } return 0; }
字典树代码(700ms)
#include <cstdio> #include <iostream> #include <cstring> using namespace std; const int MAXN=26; typedef struct Trie{ char* v; Trie *next[MAXN]; }Trie; Trie *root; void createTrie(char *str,char *str2) { int len=strlen(str); Trie *p=root,*q; for(int i=0;i<len;i++) { int id=str[i]-'a'; if(p->next[id]==NULL) { q=(Trie*)malloc(sizeof(Trie)); for(int j=0;j<MAXN;j++) q->next[j]=NULL; q->v=NULL; p->next[id]=q; p=p->next[id]; } else { p=p->next[id]; } } p->v=new char[11]; //给指针v分配一个长度为10的字符串 strcpy(p->v,str2); } char* findTrie(char *str) { int len=strlen(str); Trie *p=root; for(int i=0;i<len;i++) { int id=str[i]-'a'; p=p->next[id]; if(p==NULL) return 0; } return p->v; } int deal(Trie *T) { int i; if(T==NULL) return 0; for(int i=0;i<MAXN;i++) { if(T->next[i]!=NULL) deal(T->next[i]); } free(T); return 0; } char str1[15]; char str2[15]; char str[3100]; char res[3100]; int len; char *q; int main() { root=(Trie*)malloc(sizeof(Trie)); for(int i=0;i<MAXN;i++) root->next[i]=NULL; root->v=NULL; scanf("%s",str1); while(scanf("%s",str1)!=EOF) { if(!strcmp(str1,"END")) break; scanf("%s",str2); createTrie(str2,str1); // cout<<mp[str2]<<endl; } getchar(); gets(str); while(gets(str)) { if(!strcmp(str,"END")) break; int l=0; int r=0; for(int i=0;i<strlen(str);i++) { if(str[i]>='a'&&str[i]<='z') res[l++]=str[i]; else { res[l]=0; q=findTrie(res); if(q) { printf("%s",q); } else printf("%s",res); printf("%c",str[i]); l=0; } } printf("\n"); } deal(root); return 0; }