Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 12617 Accepted Submission(s): 4031
1 #include <iostream>
2 #include <stdio.h>
3 #include <string.h>
4 #include <malloc.h>
5 using namespace std; 6
7 struct Tire{ 8 Tire *next[26]; 9 char *trans; //定义一个指向一维数组的指针
10 Tire() //构造函数
11 { 12 int i; 13 for(i=0;i<26;i++){ 14 next[i]=NULL; 15 } 16 trans = NULL; 17 } 18 }; 19
20 Tire root; 21
22 void Insert(char trans[],char word[]) //将单词word插入到字典树中,并在最后加上翻译trans
23 { 24 Tire *p = &root; 25 int i; 26 for(i=0;trans[i];i++){ 27 int n = trans[i]-'a'; 28 if(p->next[n]==NULL) 29 p->next[n] = new Tire; 30 p = p->next[n]; 31 } 32 p->trans = (char*)malloc(sizeof(char)*11); 33 strcpy(p->trans,word); 34 } 35 void Find(char str[]) //找到对应的翻译并输出,没找到则输出原来的字符串
36 { 37 Tire *p = &root; 38 int i; 39 for(i=0;str[i];i++){ 40 int n = str[i]-'a'; 41 if(p->next[n]==NULL){ 42 printf("%s",str); 43 return ; 44 } 45 p = p->next[n]; 46 } 47 if(p->trans==NULL) 48 printf("%s",str); 49 else
50 printf("%s",p->trans); 51 } 52
53 int main() 54 { 55 char word[11],trans[11]; 56 scanf("%s",word); 57 while(scanf("%s",word)!=EOF){ //输入字典
58 if(strcmp(word,"END")==0) //遇到结束标记
59 break; 60 scanf("%s",trans); 61 Insert(trans,word); //将单词word插入到字典树中,并在最后加入其翻译
62 } 63 scanf("%s",word); 64 getchar(); 65 char str[3001]; 66 while(gets(str)){ 67 if(strcmp(str,"END")==0) 68 break; 69 int i,j=0; 70 char t[3001]={0}; 71 for(i=0;str[i];i++){ 72 if('a'<=str[i] && str[i]<='z'){ //检测到的是小写字母
73 t[j++] = str[i]; 74 } 75 else{ 76 t[j] = '\0'; 77 if(t[0]!='\0'){ //不是空的
78 Find(t); //找到对应的翻译并输出,没找到则输出原来的字符串
79 t[0]='\0',j=0; //初始化t
80 } 81 printf("%c",str[i]); 82 } 83 } 84 printf("\n"); 85 } 86
87 return 0; 88 }
Freecode : www.cnblogs.com/yym2013