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

 

转载于:https://www.cnblogs.com/jiu0821/p/4302045.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值