首页 > 代码库 > HDU 1075 map or 字典树
HDU 1075 map or 字典树
What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 12773 Accepted Submission(s): 4069
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!
题目意思大概是一种语言和英语对应,首先给你几行对应的语言和英语,以START开始END结束,每一行前面的单词为英语,后面为对应该英语的语言。
然后给你几行用该语言写的句子,让你用翻译成英语输出。
看过题后,想到了两种方法,一种是用map函数存储语言和英语的映射关系从而翻译句子。第二种是字典树,建立字典树把该语言存放到字典树中,然后翻译的时候就是查找的过程。
map函数代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <map> 5 #include <algorithm> 6 using namespace std; 7 8 main() 9 {10 map<string,string>ma;11 char a[15], b[15], s[3005];12 int n, i, j, k;13 scanf("START");14 while(scanf("%s",a)&&strcmp(a,"END")!=0)15 {16 scanf("%s",b);17 ma[b]=a;18 }19 scanf("%*s");20 getchar();21 while(gets(s)&&strcmp(s,"END")!=0)22 {23 k=0;n=strlen(s);24 for(i=0;i<n;i++)25 {26 if(s[i]>=‘a‘&&s[i]<=‘z‘)27 b[k++]=s[i];28 else{29 b[k]=‘\0‘;30 k=0;31 if(ma.find(b)!=ma.end())32 cout<<ma[b];33 else cout<<b; 34 printf("%c",s[i]);35 }36 }37 if(s[i-1]>=‘a‘&&s[i-1]<=‘z‘)38 {39 b[k]=‘\0‘;40 cout<<ma[b];41 }42 printf("\n");43 }44 45 }
字典树代码:
1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <iostream> 5 using namespace std; 6 7 char a[1000005][15]; 8 9 struct node{10 int id;11 struct node *next[26];12 node(){13 id=-1;14 memset(next,0,sizeof(next));15 } 16 }root;17 18 19 void insert(char *s,int id)20 {21 struct node *p;22 p=&root;23 int i=0;24 while(s[i])25 {26 if(p->next[s[i]-‘a‘]==0) {27 p->next[s[i]-‘a‘]=new node;28 p=p->next[s[i]-‘a‘];29 }30 else p=p->next[s[i]-‘a‘];31 i++;32 }33 p->id=id;34 }35 36 37 char *find(char *s)38 {39 struct node *p;40 p=&root;41 int i=0;42 while(s[i]&&p->next[s[i]-‘a‘])43 {44 p=p->next[s[i]-‘a‘];45 i++;46 }47 if(!s[i]&&p->id!=-1)48 return a[p->id];49 else return s;50 }51 52 main()53 {54 char b[3005], c[3005];55 int i, j, k=0;56 scanf("START");57 while(scanf("%s",a[k])&&strcmp(a[k],"END")!=0)58 {59 scanf("%s",b);60 insert(b,k);61 k++;62 }63 scanf("%s%*c",c);64 while(gets(b))65 {66 if(strcmp(b,"END")==0) break;67 i=0; k=0;68 for(i=0;b[i];i++)69 {70 if(b[i]>=‘a‘&&b[i]<=‘z‘)71 c[k++]=b[i];72 else{73 c[k]=‘\0‘;74 k=0;75 printf("%s",find(c));76 printf("%c",b[i]);77 }78 }79 if(b[i-1]>=‘a‘&&b[i-1]<=‘z‘)80 {81 c[k]=‘\0‘;82 printf("%s",find(c));83 }84 85 printf("\n");86 }87 }
对于本题,字典树效率比map高出很多。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。