HDU 1075 What is talking about (Trie)

Source: Internet
Author: User

What is talking about
Time limit:10000/5000 MS (java/others) Memory limit:102400/204800 K (java/others)
Total submission (s): 16042 Accepted Submission (s): 5198


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 中文版. Can you help him?

Inputthe problem have only one test case, the test case consists of the parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should is ignored, then some lines fo Llow, each line contains and strings, the first one is a word in 中文版, the second one is the corresponding word in Mart Ian ' s language. A line with a single string "End" indicates the end of the directory part, and this string should is ignored. The book part starts with a single line contains a string "START", this string should is ignored, then an article written In Martian ' s language. You should translate the article into 中文版 with the dictionary. If you find the word in the dictionary should translate it and write the new Word to your translation if you can ' t Find the word in the "dictionary" is not a translate it, and just copy the old Word to your translation. Space ('), tab (' \ t '), enter (' \ n ') and all the punctuation shouldNot being 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 is in the lowercase, and each word would contain at most of the characters, and each line would contain at most 3 Characters.

Outputin This problem, you have a 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!




Parsing: The original words in the dictionary are organized into trie, corresponding words as additional information of the original word into the trie tree in the last character node of the original word.




AC Code:

#include <bits/stdc++.h>//hdu C + +, this header file is not supported, g++ support # include <cstdio> #include <cstring> #include &lt  ; cctype>using namespace Std;const int maxw = 12;const int maxnode = 100000 * maxw + 10;const int sigma_size = 26;struct    trie{int ch[maxnode][sigma_size];    Char VAL[MAXNODE][MAXW];    int sz;    void Clear () {sz = 1; memset (Ch[0], 0, sizeof (ch[0]));}    int idx (char c) {return C-' a ';}        void Insert (const char *s, const char *v) {int u = 0, n = strlen (s);            for (int i=0; i<n; i++) {int c = idx (s[i]);                if (!ch[u][c]) {memset (Ch[sz], 0, sizeof (Ch[sz]));            CH[U][C] = sz++;        } u = Ch[u][c];    } strcpy (Val[u], V);        } char* Find (const char *s) {int u = 0, n = strlen (s);        int i;        Char ANS[MAXW];            for (i=0; i<n; i++) {int c = idx (s[i]);            if (!ch[u][c]) return "";        U = ch[u][c]; } return Val[u];   }};    Trie Trie;int Main () {#ifdef sxk freopen ("In.txt", "R", stdin);    #endif//Sxk trie.clear ();    Char S[maxw], SS[MAXW], text[3002];    scanf ("%s", s);        while (scanf ("%s", s)) {if (s[0] = = ' E ' && s[1] = = ' N ' && s[2] = = ' D ') break;        scanf ("%s", SS);    Trie.insert (ss, s);    } scanf ("%s", s);    GetChar ();        while (gets (text)) {if (text[0] = = ' E ' && text[1] = = ' N ' && text[2] = = ' D ') break;        int n = strlen (text); for (int i=0; i<n;)            {char FOO[MAXW];            int cnt = 0;            while (I < n && isalpha (Text[i])) {foo[cnt + +] = text[i + +];}            FOO[CNT] = ' + ';            if (!cnt) {Putchar (text[i + +]); continue;}            if (strcmp (Trie.find (foo), "")) printf ("%s", Trie.find (foo));        else printf ("%s", foo);    } puts (""); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1075 What is talking about (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.