HDU-1075-What Are You Talking About(字典树&map)

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!


题目大意:
给你一本火星词典,每个火星单词对应一个英文单词。
然后给你一篇火星文章,要求你翻译成英文。
要求如下:
如果这个火星单词用英文单词可以表示,就翻译成英文,如果没有这个单词,就原样输出。遇到标点符号或者空格原样输出即可。


代码一(map写法):

/*
    Name: hdu-1075-What Are You Talking About
    Author: long_long_ago
    Date: 18/12/15 16:59
    Description: http://acm.hdu.edu.cn/showproblem.php?pid=1075

*/

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cctype>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#define N 26
using namespace std;
map<string, string> mp;
map<string, string>::iterator it;
char s[3003];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif
    int i;
    string str, tmp;
    cin >> str;
    while(cin >> str)
    {
        if (str == "END")
            break;
        cin >> tmp;
        mp[tmp] = str;
    }
    cin >> str;
    getchar();  //ÎüÊջسµ·û 
    while(1)
    {
        gets(s);
        if (!strcmp(s, "END"))
            break;
        int len = strlen(s);
        tmp = "";
        for (i = 0; i < len; i++)
        {
            if(!isalpha(s[i]))
            {
                it = mp.find(tmp);
                if (it == mp.end())     //δÕÒµ½tmp 
                {
                    cout << tmp;
                }
                else
                {
                    cout << it->second;
                }
                cout << s[i];
                tmp = "";
            }
            else
            {
                tmp += s[i];
            }
        }
        cout << endl;
    }

    return 0;
}

代码二(字典树写法):

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 26
using namespace std;
struct Trie
{
    Trie *next[N];
    char s[11];
};
Trie *root;
void create(string str1, string str2)
{
    int i, t, j, len = str1.size();
    Trie *p = root, *tmp;
    for (i = 0; i < len; i++)
    {
        t = str1[i] - 'a';
        if (p->next[t] == NULL)
        {
            tmp = new Trie;
            for(j = 0; j < N; j++)
            {
                tmp->next[j] = NULL;
            }
            tmp->s[0] = 0;
            p->next[t] = tmp;
        }
        p = p->next[t];
    }
    len = str2.size();
    for (i = 0; i < len; i++)
    {
        p->s[i] = str2[i];
    }
    p->s[i] = 0;
}
string find(string str)
{
    int i, len = str.size(), t;
    string ss="";
    Trie* tmp = root;
    for (i = 0; i < len; i++)
    {
        t = str[i] - 'a';
        tmp = tmp->next[t];
        if(!tmp)    return str;
    }
    if (tmp->s[0])
    {
        for(i = 0; tmp->s[i]; i++)
        {
            ss += tmp->s[i];
        }
        return ss;
    }
    else
    {
        return str;
    }
}
void del(Trie* T)
{
    int i;
    if (!T)     return ;
    for (i = 0; i < N; i++)
    {
        if (T->next[i])
        {
            del(T->next[i]);
        }
    }
    delete T;
}

char s[3003];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif
    int i, len;
    string str, a;
    root = new Trie;
    for (i = 0; i < N; i++)
    {
        root->next[i] = NULL;
    }
    root->s[0] = 0;
    cin >> str;
    while(cin >> str)
    {
        if (str == "END")   break;
        cin >> a;
        create(a, str);
    }
    cin >> str;
    getchar();
    while(1)
    {
        gets(s);
        if (!strcmp(s, "END"))  break;
        len = strlen(s);
        str = "";
        for (i = 0; i < len; i++)
        {
            if (isalpha(s[i]))
            {
                str += s[i];

            }
            else
            {
                cout << find(str) << s[i];
                str = "";
            }
        }
        cout << endl;
    }
    del(root);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值