zoj1109-Language of FatMouse 【字典树】

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109

 

Language 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".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Output for Sample Input

cat
eh
loops

 

 

题意:fat有一种自己的语言,所以有自己的词典,每个fat的单词对应一个人类单词。输入是个词典。然后给你一系列fat的单词,通过词典给出对应的人类单词,如果找不到,输出eh

思路A:直接利用map映射。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <string>
 7 #include <map>
 8 using namespace std;
 9 
10 #define MAX 0x7fffffff
11 #define N 20
12 
13 map<string,string> m;
14 map<string,string>::iterator it;
15 
16 int main(){
17     //freopen("D:\\input.in","r",stdin);
18     //freopen("D:\\output.out","w",stdout);
19     char s1[N],s2[N];
20     while(cin.peek()!='\n'){
21         scanf("%s %s",s1,s2);
22         getchar();
23         m.insert(pair<string,string>(string(s2),string(s1)));
24     }
25     getchar();
26     while(gets(s1)!=NULL){
27         it=m.find(s1);
28         if(it!=m.end()){
29             printf("%s\n",(*it).second.c_str());
30         }else{
31             printf("eh\n");
32         }
33     }
34     return 0;
35 }
View Code

思路B:使用字典树,在node里另加个字符串,用来存储映射的字符串即可。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <string>
 7 #include <map>
 8 using namespace std;
 9 
10 #define MAX 0x7fffffff
11 #define N 20
12 
13 struct node{
14     node *word[26];
15     char* eng_word;
16     node(){
17         for(int i=0;i<26;i++) word[i]=NULL;
18         eng_word=NULL;
19     }
20 }*root;
21 
22 void Insert(char* s,char* st);
23 char* Find(char* s);
24 
25 int main(){
26     //freopen("D:\\input.in","r",stdin);
27     //freopen("D:\\output.out","w",stdout);
28     char s1[N],s2[N];
29     root=new node;
30     while(cin.peek()!='\n'){
31         scanf("%s %s",s1,s2);
32         getchar();
33         Insert(s2,s1);
34     }
35     getchar();
36     while(gets(s1)!=NULL){
37         printf("%s\n",Find(s1));
38     }
39     return 0;
40 }
41 void Insert(char* s,char* st){
42     int len=strlen(s);
43     node *current=root,*new_node;
44     for(int i=0;i<len;i++){
45         if(current->word[s[i]-'a']!=NULL)   current=current->word[s[i]-'a'];
46         else{
47             new_node=new node;
48             current->word[s[i]-'a']=new_node;
49             current=current->word[s[i]-'a'];
50         }
51     }
52     current->eng_word=new char[strlen(st)+1];
53     strcpy(current->eng_word,st);
54 }
55 char* Find(char* s){
56     int len=strlen(s);
57     node *current=root;
58     for(int i=0;i<len;i++){
59         if(current->word[s[i]-'a']!=NULL)   current=current->word[s[i]-'a'];
60         else    return "eh";
61     }
62     if(current->eng_word==NULL) return "eh";
63     else    return current->eng_word;
64 }
View Code

 

posted @ 2015-02-26 20:13  jiu~  阅读(458)  评论(0编辑  收藏  举报