What Are You Talking About(字典树)

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 7547    Accepted Submission(s): 2335


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
   
   
   
   
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
 

Sample Output
   
   
   
   
hello, i'm from mars. i like earth!
Hint
Huge input, scanf is recommended.
 

Author
Ignatius.L
 

这道题的题意有点不清不楚,以例子中存在于字典的一个词“fiwo”为例,当fiwo后为字母如“fiwoff”、“fiwoaf”等等时,fiwo就不翻译(因为不是独立一个单词),只要fiwo后不是字母,比如“fiwo,”、“fiwo’ ”、“fiwo   ”等等,fiwo都要翻译。

所以翻译时既要判断fiwo是否在字典树中,又要判断fiwo后面的是字母还是非字母。

AC CODE:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

struct Trie
{
    char word[11];
    struct Trie *next[26];
    Trie()
    {
        for(int i = 0; i < 26; i++)
            next[i] = NULL;
        word[0] = '\0';
    }
};

int main()
{
    char s[11], dic[11], t[3002], c;
    struct Trie *root = new Trie, *temp;
    scanf("%s%*c", s);  //START
    //构造字典树,储存字典上的单词
    while(scanf("%s", s))
    {
        if(s[0] == 'E') break;
        else
        {
            scanf("%s", dic);
            temp = root;
            for(int k = 0; dic[k] != '\0'; k++)
            {
                if(temp ->next[dic[k]-'a'] != NULL)
                    temp = temp ->next[dic[k]-'a'];
                else
                {
                    temp ->next[dic[k]-'a'] = new Trie;
                    temp = temp ->next[dic[k]-'a'];
                }
            }
            strcpy(temp ->word, s);
        }
    }
    scanf("%s%*c", s);  //START
    while(scanf("%c", &c) && c != 'E')  //先输入每一行的首字母看是否为E,不是才继续输入
    {
        //输入每一行进行翻译
        int k = 0;
        t[k++] = c;
        while(scanf("%c", &c) && c != '\n')  //输入到行末为止
        {
            t[k++] = c;
        }
        t[k] = '\n';
        t[k+1] = '\0';
        //翻译
        string str = "";
        temp = root;
        for(int j = 0; j <= k; j++)
        {
            str += t[j];
            //字母
            if(t[j] >= 'a' && t[j] <= 'z')
            {
                if(temp ->next[t[j]-'a'] != NULL)
                {
                    temp = temp ->next[t[j]-'a'];
                }
                else
                {
                    for(j++; t[j] <= 'z' && t[j] >= 'a'; j++)
                    {
                        str += t[j];
                    }
                    str += t[j];
                    cout << str;
                    str.clear();
                    temp = root;
                }
            }
            //temp ->word[0] != '\0',则t[j](已确定不是字母)之前就是字典中所存在的某一单词。
            //比如第一个例子中的“difh,”中的“,”就是此类
            else if(temp ->word[0] != '\0')
            {
                printf("%s", temp ->word);
                str.clear();
                j--;
                temp = root;
            }
            else
            {
                cout << str;
                str.clear();
                temp = root;
            }
        }//search for
    }
    return 0;
}


你可能感兴趣的:(What Are You Talking About(字典树))