Zoj 1109 Language of Fatmouse (dictionary tree)

Source: Internet
Author: User

Language of Fatmouse Time limit: Seconds Memory Limit: 32768 KB

We all know that fatmouse doesn ' t speak 中文版. But now he had to beprepared since our nation would join WTO soon. Thanks to Turing we havecomputers-to-help him.

Input specification

Input consists of 100,005 dictionary entries, followed by a blankline, followed by a message of the up to 100,005 words. Each dictionaryentry are a line containing an 中文版 word, followed by a space and a fatmouse word. No Fatmouse Word appears more than once in thedictionary. The message is a sequence of words in the language of Fatmouse,one word on each line. Each word in the input was a sequence of at most 10lowercase letters.

Output specification

Output is the message translated to 中文版, one word per line. Fatmouse words not in the dictionary should is translated as "eh".

Sample Input
Dog Ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay
Output for Sample Input
Cateh

Loops

Jianke: The dictionary tree, the end of the word insertion record the word corresponding to the subscript, query can.

#include <cstring> #include <algorithm> #include <cstdio> #include <iostream> #include <    Cstdlib> #define N 100020using namespace Std;char s[12];char ans[n][12];struct Trie {int id;    struct Trie *nxt[26];        Trie () {id=0;        for (int i=0; i<26; i++) {nxt[i]=null;    }}};void Trie_inser (Trie *p,char s[],int id) {int i=0;    Trie *q=p;        while (S[i]) {int nx=s[i]-' a ';        if (q->nxt[nx]==null) {q->nxt[nx]=new Trie;        } i++;    q=q->nxt[nx]; } Q->id=id;}    int Trie_serch (Trie *p,char s[]) {Trie *q=p;    int i=0;        while (S[i]) {int nx=s[i]-' a ';        if (Q->nxt[nx]==null) return 0;        q=q->nxt[nx];    i++; } return q->id;}    int main () {//freopen ("test.in", "R", stdin);    Trie *p=new Trie;    int id=1;    Char a[40];        while (1) {gets (a);        if (a[0]== ') break;        int l=0;        int Len=strlen (a);        int i; for (i= 0; i<len;            i++) {if (a[i]== ') break;        Ans[id][l++]=a[i];        } ans[id][l]= ' + ';        l=0;        i++;        for (; i<len; i++) {s[l++]=a[i];        } s[l]= ' + ';        printf ("%s%s\n", ans[id],s);        Trie_inser (P,s,id);    id++;        } while (~SCANF ("%s", s)) {id=trie_serch (p,s);        if (id==0) {printf ("eh\n");        } else {printf ("%s\n", Ans[id]); }} return 0;}


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

Zoj 1109 Language of Fatmouse (dictionary tree)

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.