Poj-2503-Babelfish

Source: Internet
Author: User

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 shocould be translated as "eh ".

Sample Input

dog ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay

Sample output

catehloops

Hint

Huge input and output, scanf and printf are recommended.


What is a dictionary tree?

Baidu knows it now ------> the address is: trie tree

I prefer to use arrays to write dictionary trees. I put two pictures here!

Hope to help you understand it!

It is required that we translate words from a planet into corresponding English. If there is one word, we will output the corresponding English. If there is no word, we will not output it!

Idea: first, we need to use the word of a planet to create a dictionary tree, and then put the corresponding word "behind the leaf node". We can compare it to an earring with a pendant! Then you can directly search for the translation!

Then, the problem with the initial input is solved!

Gets (STR) uses '\ n' as the Terminator. In addition, it can also be read into the carriage return, so gets (STR) is used as the input, with two for loops, you can easily separate the English words from a certain ball of words. No other optimization is made here! Then we can make achievements!



The AC code is as follows:




# Include <stdio. h> # include <string. h ># include <iostream> using namespace STD; const int maxn = 201314; struct tree {int next [26]; char wei [26]; // Add a tail to the end of the corresponding tree! Void Init () {memset (next,-1, sizeof (next) ;}t [maxn]; int cur = 1; void creat (char ss [], int Len, char CI []) {int p = 0; For (INT I = 0; I <Len; I ++) {int x = ss [I]-'A'; If (T [p]. next [x] =-1) {T [cur]. init (); t [p]. next [x] = cur ++;} p = T [p]. next [X];} strcpy (T [p]. wei, CI);} void Cha (char ss []) {int p = 0, I = 0; while (ss [I]) {int x = ss [I]-'A'; If (T [p]. next [x] =-1) {cout <"eh" <Endl; return;} p = T [p]. next [X]; I ++;} c Out <t [p]. Wei <Endl;} int main () {T [0]. INIT (); // Initialization is required! Char str1 [2013], str2 [2014]; while (gets (str1) {If (! Strlen (str1) break; char Qian [2013], Hou [2014]; int p = 0; while (str1 [p]! = '') {Qian [p] = str1 [p]; P ++; // extract the previous word!} Qian [p] = '\ 0'; int CNT = 0; For (INT I = p + 1; I <strlen (str1); I ++) hou [CNT ++] = str1 [I]; // extract the following word! Hou [CNT] = '\ 0'; creat (Hou, strlen (Hou), Qian);} while (gets (str2 )! = NULL) Cha (str2); Return 0 ;}


Poj-2503-Babelfish

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.