hdu 1075 What Are You Talking About(Trie树)

来源:互联网 发布:知合控股有限公司 编辑:程序博客网 时间:2024/05/31 18:40

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 should 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 should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should 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 should 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!
Hint
Huge input, scanf is recommended.

Trie树入门题

输入一段英文,对照给的单词的映射,输出映射后的字符串,注意是字符串整体映射,而不是单独的一个字符映射,将映射的字符串用来建树,每个单词结束的地方保存一个目的字符串。这样在查找到结尾的时候就可以直接输入映射后的字符串,如果没有找到到就输出原字符。

#include <iostream>#include <stdio.h>#include <string.h>using namespace std;typedef struct trie{    char *str;    struct trie *next[26];    trie()    {        str=NULL;        memset(next, 0, sizeof(next));    }    }Trie;bool flag;void BuildTrie(Trie *&root,char *str1,char *str2) //建树{    Trie *p=root;    Trie *q;    int len=strlen(str2);    int j;    int count=0;    for (int i=0; i<len; i++)    {        j=str2[i]-'a';        if(!p->next[j])        {            q=new Trie();            p->next[j]=q;        }        p=p->next[j];    }    p->str=new char[15];    strcpy(p->str, str1); //保存映射后的字符串}void Find(Trie *root,char *s){    int len=strlen(s);    int j;    for (int i=0; i<len; i++)    {        j=s[i]-'a';        if(root->next[j]==NULL) //没查找到        {            printf("%s",s);            return;        }        else        {            root=root->next[j];        }    }    if(root->str==NULL) //不是完整的映射过程    {        printf("%s",s);    }    else    {        printf("%s",root->str); //输入映射后的字符串    }}void DeleteTrie(Trie *&root) //释放空间{    for (int i=0; i<=9; i++)    {        if(root->next[i])        {            DeleteTrie(root->next[i]);        }    }    delete root;}int main(){    char str1[3003],str2[3003];    bool flag;    int j,len;    Trie *root=new Trie();    scanf("%s",str1);    while(scanf("%s",str1) && strcmp(str1, "END")!=0)    {        scanf("%s", str2);        BuildTrie(root,str1, str2);    }    scanf("%s",str1);    getchar();    while(gets(str1) && strcmp(str1,"END")!=0)    {        len = strlen(str1);        str1[len]='\n';   //处理最后一组数据        len++;        flag = false;        j = 0;        for(int i =0; i<len; i++)        {            if(str1[i] >= 'a' && str1[i] <= 'z')            {                if(!flag)                {                    flag = true;                }                str2[j++] = str1[i];            }            else   //被非字母所分割            {                if(flag)                {                    flag =false;                    str2[j] = '\0';                    j = 0;                    Find(root,str2);                }                printf("%c", str1[i]);            }        }    }    DeleteTrie(root);    return 0;}



0 0