HDU-What Are You Talking About (Dictionary tree)
What Are You Talking AboutTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission (s): 15100 Accepted Submission (s): 4850
Problem Description Ignatius is so lucky that he met a Martian yesterday. but he didn't know the language the Martians use. the Martian gives him a history book of Mars and a dictionary when it leaves. now Ignatius want to translate the history book into English. can you help him?
Input The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. the dictionary part starts with a single line contains a string "START", this string shoshould be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string shoshould be ignored. the book part starts with a single line contains a string "START", this string shoshould be ignored, then an article written in Martian's language. you shoshould translate the article into English with the dictionary. if you find the word in the dictionary you shoshould translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. space (''), tab ('\ t'), enter (' \ n') and all the punctuation shoshould not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. all the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
Output In this problem, you have to output the translation of the history book.
Sample Input
STARTfrom fiwohello difhmars riwosfearth fnnvklike fiiwjENDSTARTdifh, i'm fiwo riwosf.i fiiwj fnnvk!END
Sample Output
hello, i'm from mars.i like earth!HintHuge input, scanf is recommended.
The author met Miss Mars, but he was forced to understand what she said, so miss Mars gave him a Mars-English dictionary. Now we provide many English-Mars word pairs. You need to translate the Mars sentence.
English. Space (''), tab ('\ t'), enter (' \ n'), punctuation, translation, and Mars that cannot be translated.
Analysis: You can use the dictionary tree to solve the problem and process words in a sentence with a little thought.
Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1075
Code List:
# Include
# Include
# Include
# Includeusing namespace std; const int MAX = 27; const int maxn = 30005; const int Max = 1000005; struct trie {int point; trie * next [MAX];}; trie * root = new trie; char s [maxn], ss [MAX]; char anStr [Max] [15]; int slen, k = 1, ak, aj; void createTrie (char * s, int pose) {trie * p = root, * q; int len = strlen (s), pos; for (int I = 0; I
Next [pos] = NULL) {q = new trie; q-> point = 0; for (int j = 0; j
Next [j] = NULL; p-> next [pos] = q; p = p-> next [pos];} else {p = p-> next [pos] ;}} p-> point = pose; // The end of the string and the size is the subscript of the array where the word is located .} Int findTrie (char * s) {trie * p = root; int len = strlen (s), pos; for (int I = 0; I
Next [pos] = NULL) return 0; p = p-> next [pos];} return p-> point;} void delTrie (trie * Root) {for (int I = 0; I
Next [I]! = NULL) delTrie (Root-> next [I]);} free (Root);} int main () {for (int I = 0; I
Next [I] = NULL; while (gets (s) {if (strcmp (s, "END") = 0) break; if (strcmp (s, "START") = 0) continue; slen = strlen (s); for (int I = 0; I
= 'A' & s [I] <= 'Z') | s [I] = '\'') {ss [ak ++] = s [I]; aj = 0;} else {if (! Aj) {int judge = findTrie (ss); if (judge) printf ("% s", anStr [judge]); else printf ("% s", ss ); memset (ss, '\ 0', sizeof (ss); ak = 0;} aj ++; printf ("% c", s [I]);} printf ("\ n");} delTrie (root); return 0 ;}