hud 1075 What Are You Talking About (字典树或map)

http://acm.hdu.edu.cn/showproblem.php?pid=1075
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.
题目大意:先给你一个字典,前边是英语后边的是火星语,start表示字典开始,end表示字典结束,第二次的start表示对话开始,将对话中字典中出现的火星语翻译成英语,其余都不变。注意字典和对话都是小写字母,所有的start和end都不做处理。
解题思路:
1.字典树解法:用火星文够着字典树,在形成单词的那个flag标记处再加一个字符串,表示其对应的英语。其余按照线段树的方法查询就可以了。
2.map解法:用火星语当做关键字,和英语形成映射,在查询时对于每次出现的火星语看是否在关键字中出现过,如果出现过就输出对应的英语,否则就原样输出。
字典树解法:

#include <bits/stdc++.h>

using namespace std;

struct Trie
{
    int is_str;
    string str;///str来保存火星文对应的英语
    Trie *child[26];
    Trie()
    {
        is_str = 0;
        str="";
        for(int i=0;i<26;i++)
            child[i] = NULL;
    }
};
Trie *root,*current,*nextt;
void insert(string s,string ans)
{
    current = root;
    for(int i = 0;i<s.length();i++){
        if(current->child[s[i]-'a'] == NULL){
            nextt = new Trie;
            current->child[s[i]-'a'] = nextt;
            current = nextt;
        }
        else{
            current = current->child[s[i]-'a'];
        }
    }
    current->is_str = 1;
    current->str = ans;///在标记的同时记录一下它所对应的英文
}
int search(string s)
{
    current = root;
    string ss = "";
    for(int i=0;i<s.length()&&current!=NULL;i++)
    {
        current = current->child[s[i]-'a'];
    }
    if(current!=NULL&&current->is_str==1)
        return 1;
    return 0;
}
void del(Trie *root)
{
    for(int i=0;i<26;i++)
        if(root->child[i]!=NULL)
            del(root->child[i]);
    delete(root);
}
int main()
{
    string str;int flag_s = 0,flag_e = 0;
    root = new Trie;
    while(getline(cin,str))
    {
        if(str=="START"){
                flag_s++;
            continue;
        }
        if(str=="END"){
            flag_e++;
            if(flag_e ==2)
                break;
            continue;
        }
        if(flag_s == 1)
        {
            stringstream ss(str);
            string s,a[2];int i = 0;
            while(ss>>s)
                a[i++]=s;
           insert(a[1],a[0]);
        }
        if(flag_s == 2)
        {
           string s;
           for(int i=0;i < str.length();i++)
           {
               if(str[i] >='a'&&str[i]<='z')
               {
                   s+=str[i];
                   if(str[i+1] <'a'||str[i+1]>'z')
                   {
                       if(search(s))
                            cout<<current->str;
                       else
                            cout<<s;
                       s.clear();
                   }
               }
               else
                cout<<str[i];
           }
           cout<<endl;

        }
    }del(root);
    return 0;
}

map解法:

#include <bits/stdc++.h>

using namespace std;
map<string,string> m;
int main()
{
    string str;int flag_s = 0,flag_e = 0;
    ///输入处理,每次读入一行flag_s表示start出现次数flag_e表示end出现次数
    while(getline(cin,str))
    {
        if(str=="START"){
                flag_s++;
            continue;
        }
        if(str=="END"){
            flag_e++;
            if(flag_e ==2)
                break;
            continue;
        }
        if(flag_s == 1)
        {
            ///当第一个start出现后第二个没有出现将输入的一行字符按空格分开,然后让火星文映射英文
            stringstream ss(str);
            string s,a[2];int i = 0;
            while(ss>>s)
                a[i++]=s;
            m[a[1]] = a[0];
        }
        if(flag_s == 2)
        {
           string s;
           ///当第二个start出现后,将这个字符串中每个单词分离,看它有没有在关键字中出现过,如果出现过就输出它的映射,否则输出本身
           for(int i=0;i < str.length();i++)
           {
               if(str[i] >='a'&&str[i]<='z')
               {
                   s+=str[i];
                   if(str[i+1] <'a'||str[i+1]>'z')
                   {
                       if(m.count(s))
                            cout<<m[s];
                       else
                            cout<<s;
                       s.clear();///注意这里的字符串每次用过之后都要清零
                   }
               }
               else
                cout<<str[i];///如果是其他字符就原样输出
           }
           cout<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值