HDU ---- (1075) What are you talking about (search for Trie)

Source: Internet
Author: User
Tags define local
What are you talking about

Time Limit: 10000/5000 MS (Java/others) memory limit: 102400/204800 K (Java/Others)
Total submission (s): 14107 accepted submission (s): 4546


Problem descriptionignatius 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?

 

Inputthe 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.

 

Outputin this problem, you have to output the translation of the history book.

 

Sample inputstartfrom fiwohello difhmars riwosfearth fnnvklike fiiwjendstartdifh, I'm fiwo riwosf. I fiiwj fnnvk! End

 

Sample outputhello, I'm from Mars. I like Earth! HintHuge input, scanf is recommended.

 

Authorignatius. L

 

Recommend is to give a text of Mars, then correspond to the translation of Earth, and then give a text that contains the text of Mars and Earth, you need to translate it into Earth (if it is earth text, this is a direct output, and vice versa .) Train of Thought: in fact, you only need to make the Mars into a dictionary tree (and fill in a tail at the end of each word so that it can be searched accordingly .), Search for each word. If yes, translate the word directly. If not, output the word directly. Code:
1/* HDU 1075 dictionary tree Writing Method */2 // # define local 3 # include <cstdio> 4 # include <cstring> 5 # include <cstdlib> 6 typedef struct node 7 {8 int ID; 9 struct node * child [26]; 10} trie; 11 void insert (char * s, trie * root, int v) 12 {13 trie * cur = root, * curnew; 14 int I, Pos; 15 for (I = 0; s [I]! = '\ 0'; I ++) {16 Pos = s [I]-'A'; 17 if (cur-> child [POS] = NULL) 18 {19 curnew = new trie; 20 curnew-> id = 0; 21 for (Int J = 0; j <26; j ++) 22 curnew-> child [J] = NULL; 23 cur-> child [POS] = curnew; 24} 25 cur = cur-> child [POS]; 26} 27 cur-> id = V; 28} 29 int query (char * s, trie * root) 30 {31 trie * cur = root; 32 int I, Pos; 33 for (I = 0; s [I]! = '\ 0'; I ++) {34 Pos = s [I]-'A'; 35 if (cur-> child [POS]! = NULL) 36 cur = cur-> child [POS]; 37 Else return 0; // output original string 38} 39 return cur-> ID; 40} 41 char AA [2, 700000] [11], BB [11]; 42 char STR [3010]; 43 int main () 44 {45 # ifdef local46 freopen ("test. in "," r ", stdin); 47 # endif48 trie * root = new trie; 49 root-> id = 0; 50 int st; 51 for (INT I = 0; I <26; I ++) 52 root-> child [I] = NULL; 53 scanf ("% s", AA [0]); 54 int I = 1; 55 while (scanf ("% s", AA [I]), strcmp (AA [I], "end") 56 {57 scanf ("% s ", bb); 58 Insert (BB, root, I); // corresponding number 59 I ++; 60} 61 scanf ("% s", AA [0]); 62 getchar (); 63 while (gets (STR), strcmp (STR, "end") 64 {65 for (ST = I = 0; STR [I]! = '\ 0'; I ++) 66 {67 If (STR [I] <'A' | STR [I]> 'Z ') {68 if (I> st) {69 strncpy (AA [0], STR + st, I-St ); 70 AA [0] [I-st] = '\ 0'; 71 printf ("% s", AA [query (AA [0], root)]); 72} 73 ST = I + 1; 74 printf ("% C", STR [I]); 75} 76} 77 puts (""); 78} 79 return 0; 80}

Of course, you can use map. I will not add it here! Meow! O (∩) O Haha ~

HDU ---- (1075) What are you talking about (search for Trie)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.