思路:就是用一个字典树翻译单词的问题,我们用题目中给出的看不懂的那些单词建树,这样到每个单词的叶子结点中存放原来对应的单词就好。
这样查询到某个单词时输出叶子结点存的就行,查不到就"en"呗。这题用hash也是可以的
1 #include<iostream> 2 #include<cstdio> 3 #include<stdio.h> 4 #include<cstring> 5 #include<cmath> 6 #include<vector> 7 #include<stack> 8 #include<map> 9 #include<set> 10 #include<list> 11 #include<queue> 12 #include<string> 13 #include<algorithm> 14 #include<iomanip> 15 using namespace std; 16 17 struct node 18 { 19 int cnt; 20 char c[26];//结点所对应的字符 21 struct node *next[26]; 22 node () 23 { 24 cnt = 0; 25 memset(next,0,sizeof(next)); 26 } 27 }; 28 node * root = NULL;//根结点初始为NULL 29 30 void BuildTrie(char *s,char *temp) 31 { 32 node *p = root; 33 node *tmp = NULL; 34 int l = strlen(s); 35 for(int i = 0;i < l ;i ++) 36 { 37 if(p->next[s[i]-'a'] == NULL) 38 { 39 tmp = new node; 40 p->next[s[i]-'a'] = tmp; 41 42 } 43 p = p->next[s[i]-'a']; 44 } 45 p->cnt = 1; 46 strcpy(p->c,temp);//存放翻译结果 47 48 } 49 50 void Query(char *s) 51 { 52 node *p = root; 53 int l = strlen(s); 54 for(int i = 0 ;i< l ;i++) 55 { 56 if(p->next[s[i]-'a'] == NULL) 57 { 58 printf("eh "); 59 return ; 60 } 61 p = p->next[s[i]-'a']; 62 } 63 printf("%s ",p->c); 64 return ; 65 } 66 67 void Del(node * root) 68 { 69 for(int i = 0;i < 26;i++) 70 { 71 if(root->next[i]) 72 { 73 Del(root->next[i]); 74 } 75 } 76 } 77 78 int main() 79 { 80 char str[30],s1[15],s2[15]; 81 root = new node; 82 while(gets(str)) 83 { 84 if(str[0] == '