1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include <malloc.h>
5 #include <ctype.h>
6 struct Tries{
7 Tries *next[26];
8 char *str;
9 Tries(){
10 memset(next,NULL,sizeof(next));
11 str=NULL;
12 }
13 };
14 Tries *tree;
15 inline void insert(char eng[],char mars[]){
16 Tries *temp=tree;
17 int len=strlen(mars);
18 for(int i=0;i<len;i++){
19 int k=mars[i]-'a';
20 if(temp->next[k]==NULL)
21 temp->next[k]=new Tries();
22 temp=temp->next[k];
23 }
24 temp->str=new char[11];
25 strcpy(temp->str,eng);
26 }
27
28 inline bool find(char mars[],char eng[]){
29 Tries *temp=tree;
30 int len=strlen(mars);
31 for(int i=0;i<len;i++){
32 int k=mars[i]-'a';
33 if(temp->next[k]==NULL)
34 return false;
35 temp=temp->next[k];
36 }
37 if(temp->str==NULL)//此处关键 WA了四次吧
38 return false;
39 strcpy(eng,temp->str);
40 return true;
41 }
42 int main(){
43 //freopen("in.txt","r",stdin);
44 tree=new Tries();
45 char eng[11],mars[11];
46 gets(eng);
47 while(scanf("%s %s",eng,mars)){
48 if(strcmp(eng,"END")==0)
49 break;
50 getchar();
51 insert(eng,mars);
52 }
53 getchar();
54 char line[3005];
55 while(gets(line)){
56 if(strcmp(line,"END")==0)
57 break;
58 char word[11],translate[11];
59 int i,j=0;
60 int len=strlen(line);
61 for(i=0;i<len;i++){
62 if(islower(line[i]))
63 word[j++]=line[i];
64 else{
65 if(j>0){
66 word[j]='\0';
67 if(find(word,translate))
68 printf("%s",translate);
69 else
70 printf("%s",word);
71 j=0;
72 }
73 printf("%c",line[i]);
74 }
75 }
76 printf("\n");
77 }
78 return 0;
79 }
posted on 2012-05-15 21:26
Leo.W 阅读(251)
评论(0) 编辑 收藏 引用