poj 2503 Babelfish
Description
You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
Input
Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output
Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
Sample Input
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
Sample Output
cat eh loops
Hint
Huge input and output,scanf and printf are recommended.
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 }