HDOJ-1075 What Are You Talking About 字典树查找

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 7414    Accepted Submission(s): 2292


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!
 
  1 /* 功能Function Description:     HDOJ-1075 字典树
  2    开发环境Environment:          DEV C++ 4.9.9.1
  3    技术特点Technique:
  4    版本Version:
  5    作者Author:                   可笑痴狂
  6    日期Date:                      20120810
  7    备注Notes:
  8 */
  9 
 10 
 11 //代码一:-------杭电上这个代码有时候能提交,有时候显示  内存访问非法,不知道啥原因
 12 //islower(ch)用以判断ch中的字母是否为小写字母,issupper(c) 判断是否为大写字母。
 13 #include<stdio.h>
 14 #include<string.h>
 15 #include<malloc.h>
 16 #include<ctype.h>
 17 
 18 typedef struct node
 19 {
 20     char s[50];
 21     struct node *next[26];
 22 }node;
 23 
 24 node memory[1000000];
 25 int k=0;
 26 
 27 void insert(char *b,char *a,node *T)   //用b插入字典树,并将b对应的译文a放在b的末节点中
 28 {
 29     int i,len,j,id;
 30     node *p,*q;
 31     p=T;
 32     len=strlen(b);
 33     for(i=0;i<len;++i)
 34     {
 35         id=b[i]-'a';
 36         if(p->next[id]==NULL)
 37         {
 38         //    q=(node *)malloc(sizeof(node));
 39             q=&memory[k++];
 40             memset(q->s,'\0',sizeof(q->s));
 41             for(j=0;j<26;++j)
 42                 q->next[j]=NULL;
 43             p->next[id]=q;
 44         }
 45         p=p->next[id];
 46     }
 47     strcpy(p->s,a);
 48 }
 49 
 50 int search(char *t,node *T)   //在字典树中查找t,找到的话,输出译文并返回1,否则返回0
 51 {
 52     int id,i=0;
 53     char *p=t;
 54     node *q=T;
 55     while(islower(p[i]))
 56     {
 57         id=p[i]-'a';
 58         if(q->next[id]==NULL) //未找到
 59             return 0;
 60         else
 61         {
 62             q=q->next[id];
 63             ++i;
 64         }
 65     }
 66     if(strlen(q->s))      //s中不空 说明存在相应的翻译,直接输出译文
 67     {
 68         printf("%s",q->s);
 69         return 1;
 70     }
 71     return 0;
 72 }
 73 
 74 int main()
 75 {
 76     int i,len;
 77     char a[50],b[50];
 78     char c[4000];
 79     node *T;
 80 //    T=(node *)malloc(sizeof(node));
 81     T=&memory[k++];
 82     memset(T->s,'\0',sizeof(T->s));
 83     for(i=1;i<26;++i)
 84         T->next[i]=NULL;
 85     while(scanf("%s",a)&&strcmp(a,"START")==0)
 86     {
 87         while(scanf("%s",a)&&strcmp(a,"END"))
 88         {
 89             scanf("%s",b);
 90             insert(b,a,T);
 91         }
 92         while(scanf("%s",a)&&strcmp(a,"START")==0)
 93         {
 94             getchar();
 95             while(gets(c))
 96             {
 97                 if(strcmp(c,"END")==0)
 98                     return 0;
 99                 len=strlen(c);
100                 for(i=0;i<len;++i)
101                 {
102                     if(!islower(c[i]))
103                         printf("%c",c[i]);
104                     else
105                     {
106                         if(!search(&c[i],T))
107                         {
108                             while(islower(c[i]))
109                                 printf("%c",c[i++]);
110                             --i;
111                         }
112                         else
113                         {
114                             while(islower(c[i++]));
115                             i-=2;
116                         }
117                     }
118                 }
119                 printf("\n");
120             }
121         }
122     }
123     return 0;
124 }
125 
126 //网上的代码:
127 //代码一:-------字典树
128 #include<iostream>
129 #include<string>
130 using namespace std;
131 int i;
132 struct dictree
133 {
134     dictree *child[26];
135     char engWord[11];
136     dictree()//初始化非常的有必要
137     {
138         for(i=0;i<26;i++)
139             child[i] = NULL;
140         engWord[0]='\0';
141     }
142 };
143 dictree root;
144 void insert(char eng[],char mar[])//构建字典树
145 {
146     dictree *now = &root;
147     char *tmp = mar;
148     while(*tmp)
149     {
150         if(now->child[*tmp-'a']==NULL)
151             now->child[*tmp-'a'] = new dictree;
152         now = now->child[*tmp-'a'];
153         tmp++;
154     }
155     strcpy(now->engWord,eng);    
156 }
157  
158 char *find(char ch[])//查找单词
159 {
160     dictree *p = &root;
161     int k=0;
162     while(1)
163     {
164         if(ch[k]=='\0' || p->child[ch[k]-'a']==NULL)
165             break;
166         p = p->child[ch[k]-'a'];
167         k++;
168     }
169     if(ch[k]=='\0' && strcmp(p->engWord,"")!=0)
170         return p->engWord;
171     return NULL;
172 }
173  
174 int main()
175 {
176     char a[11],b[11];
177     scanf("%s",a);//"START"
178     while(scanf("%s",a) && strcmp(a,"END")!=0 )
179     {
180         scanf("%s",b);
181         insert(a,b);
182     }
183     scanf("%s",a);//"START"
184     getchar();//吃回车
185     char tmp[3002];
186     while(1)
187     {
188         gets(tmp);//用这个比getline()强
189         if(strcmp(tmp,"END") == 0 )
190             break;
191         int i,len,k=0;
192         len = strlen(tmp);
193         tmp[len]=' ';//多加一个' ',输出的时候注意
194         tmp[++len]='\0';
195         char tp[3002];
196         for(i=0;i<len;i++)
197         {
198             if(!(tmp[i]>='a' && tmp[i]<='z'))//非小写字母
199             {
200                 tp[k]='\0';
201                 char *temp = find(tp);//查找是否有对应的engWord
202                 if(temp)//存在这个单词
203                     printf("%s",temp);
204                 else                                                    
205                     printf("%s",tp);//可以用cout<<tp;//不可以用puts(tp);用puts有换行
206                 k=0;
207                 if(i!=len-1)//最后有一个' '是多余的
208                     cout<<tmp[i];//输出非小写字母字符
209             }
210             else //小写字母
211                 tp[k++]=tmp[i];
212         }
213         cout<<endl;
214     }
215     return 0;
216 } 
217 
218 
219 
220 // 代码二:---STL map
221 #include<iostream>
222 #include<string>
223 #include<map>
224 using namespace std;
225 map<string,string>M;
226 int main()
227 {
228     string a,b;
229     cin>>a;//"START"
230     while(cin>>a && a!="END")
231     {
232         cin>>b;
233         M[b] = a;
234     }
235     cin>>a;//"START"
236     getchar();//吃回车
237     char tmp[3005];
238     while(1)
239     {
240         gets(tmp);//用这个比getline()强
241         if(strcmp(tmp,"END") == 0 )
242             break;
243         int i,len;
244         len = strlen(tmp);
245         b = "";
246         for(i=0;i<len;i++)
247         {
248             if(!(tmp[i]>='a' && tmp[i]<='z'))//非小写字母
249             {
250                 if(M[b]!="")//存在这个单词
251                     cout<<M[b];
252                 else
253                     cout<<b;
254                 b="";
255                 cout<<tmp[i];
256             }
257             else //小写字母
258                 b+=tmp[i];
259         }
260         cout<<endl;
261     }
262     return 0;
263 } 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值