avatar
fireworks99
keep hungry keep foolish

HDU 1075 What are you Talking About

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?

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1075

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\存字典,map\标记是否字典里能查到

输出时,遍历截取(substr)每个单词(以小写字母为标准)

关于string的substr

假设:string s = “0123456789”;

string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = “56789”

string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位:sub2 = “567”

Code

#include <map>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

map<string, string> mp;
map<string, bool> vis;

int main()
{
    string s1, s2;
    cin >> s1;///必定是START
    while(1)
    {
        cin >> s1;
        if(s1 == "END")
            break;
        cin >> s2;
        mp[s2] = s1;
        vis[s2] = 1;
//        cout << "s1: " << s1 << '\n' << "s2: " << s2 << '\n';
    }
    cin >> s1;///必定是START
    getchar();///后面紧跟有getline(能读入回车),此处读回车避免错误
    string tem;
    while(1)
    {
        getline(cin, tem);
        if(tem == "END")
            break;
        for(int i = 0, j = 0; i < tem.length(); i = j)
        {
            if(!(tem[i] >= 'a' && tem[i] <= 'z'))
            {
                cout << tem[i];
                j++;
            }
            else
            {
                while(tem[j] >= 'a' && tem[j] <= 'z')///拓展上界
                    j++;
                string s = tem.substr(i, j - i);
                if(vis[s])
                    cout << mp[s];
                else
                    cout << s ;
            }
        }
        cout << '\n';
    }
    return 0;
}
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy