zoj 1109-Language of FatMouse【辞書ツリー】

16165 ワード

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()!='
'){ 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
",(*it).second.c_str()); 30 }else{ 31 printf("eh
"); 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()!='
'){ 31 scanf("%s %s",s1,s2); 32 getchar(); 33 Insert(s2,s1); 34 } 35 getchar(); 36 while(gets(s1)!=NULL){ 37 printf("%s
",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