What Are You Talking About

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

在处理这个题目前,先了解一些C语言关于输入输出的概念:

scanf( )函数和gets( )函数都可用于输入字符串,但在功能上有区别。若想从键盘上输入字符串"hi hello",则应该使用 gets 函数。

gets可以接收空格;而scanf遇到空格、回车和Tab键都会认为输入结束,所有它不能接收空格。

char string[15]; gets(string); /*遇到回车认为输入结束*/

scanf("%s",string); /*遇到空格认为输入结束*/

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
using namespace std;
char str[10000];
int main()
{
    map<string,string>dic;
    string a,b;
    cin>>a;
    while(cin>>a){
        if(a=="END")
            break;
        cin>>b;
        dic[b]=a;
    }
    cin>>a;
    getchar();
    //gets可以接收空格
    while(gets(str)){
        if(strcmp(str,"END")==0){
            break;
        }
        string ans="";
        for(int i=0;i<strlen(str);i++){
            if(str[i]>='a'&&str[i]<='z') ans+=str[i];
            else if(ans!=""){//遍历到当前字符非a-z,且ans中有值,则输出ans中暂存的值,同时将当前字符(空格或标点符号)输出
                if(dic[ans]!=""){//如果ans中存的单词能在dic中找到,则输出dic
                    cout<<dic[ans];
                }else
                    cout<<ans;
                ans="";
                cout<<str[i];//输出标点符号或空格
            }else
                cout<<str[i];
        }
        cout<<endl;
    }
    return 0;
}
posted @ 2019-07-13 15:49  里昂静  阅读(1056)  评论(0编辑  收藏  举报