标签:
Description
Input
Output
Sample Input
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
Sample Output
cat eh loops
Hint
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 typedef struct Trie 5 { 6 int num; 7 struct Trie *next[26]; 8 bool exist; 9 char ss[11]; 10 }Node,*trie; 11 Node *create() 12 { 13 Node* node=(Node *)malloc(sizeof(Node)); 14 node->num=0; 15 node->exist=false; 16 memset(node->next,0,sizeof(node->next)); 17 return node; 18 } 19 void insert_(trie root,char *mars,char *en) 20 { 21 trie node=root; 22 char *p=mars; 23 int id; 24 while(*p) 25 { 26 id=*p-‘a‘; 27 if(node->next[id]==NULL) 28 { 29 node->next[id]=create(); 30 } 31 node=node->next[id]; 32 ++p; 33 node->num+=1; 34 } 35 node->exist=true; 36 strcpy(node->ss,en); 37 //printf("B %s\n",en); 38 //printf("A %s\n",node->ss); 39 } 40 char* finds(trie root,char *str) 41 { 42 43 trie node=root; 44 char *p=str; 45 int id; 46 while(*p) 47 { 48 id=*p-‘a‘; 49 node=node->next[id]; 50 ++p; 51 if(node==NULL) 52 return NULL; 53 } 54 if(node->exist){ 55 //printf("%s\n",node->ss); 56 return node->ss; 57 } 58 else 59 return NULL; 60 } 61 int main() 62 { 63 trie root=create(); 64 char str1[10],str2[10],*p,ch; 65 //scanf("%s",str1); 66 while(scanf("%s",str1)) 67 { 68 //printf("%s\n",str1); 69 scanf("%s",str2); 70 insert_(root,str2,str1); 71 getchar(); 72 ch=getchar(); 73 if(ch!=‘\n‘) 74 { 75 ungetc(ch,stdin); 76 continue; 77 } 78 break; 79 } 80 //printf("%s\n",finds(root,str1)); 81 while(scanf("%s",str1)!=EOF) 82 { 83 p=finds(root,str1); 84 if(p) 85 printf("%s\n",p); 86 else printf("eh\n"); 87 } 88 return 0; 89 }
标签:
原文地址:http://www.cnblogs.com/PrayG/p/5899664.html