HDU-1075 What Are You Talking About

HDU-1075 What Are You Talking About

Time Limit : 10000/5000ms (Java/Other)
Memory Limit : 102400/204800K (Java/Other)

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.
[/hint]


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

分析

题意:将一个单词翻译成对应的单词输出,如果没有对应的单词就原样输出,首先输入一个START表示开始,然后下面每行输入2个单词中间用空格分开,表示第一个单词翻译成第二个单词,然后以一个单词END表示结束。下面同样是以START表示开始,然后输入多行,以END表示结束,输出翻译后的语句。

思路:原本我是在想用字典树实现的,由于实力不够,没写出来,最后改用了map进行实现。这一题基本上看一下代码就可以理解了。

代码

#include <iostream>
#include <stdio.h>
#include <map>
#include <string>
#include <string.h>
using namespace std;
int main()
{
    char s[15],ss[15];
    map<string,string>mp;
    while (~scanf("%s",s))
    {
        if (strcmp(s,"START")==0)
            continue;
        if (strcmp(s,"END")==0)
            break;
        scanf("%s",ss);
        mp[ss]=s;
    }
    getchar();
    char str[3005];
    while (gets(str))
    {
        if (strcmp(str,"START")==0)
            continue;
        if (strcmp(str,"END")==0)
            break;
        int l=0,r=0;
        char res[3005];
        for (int i=0;i<strlen(str);i++)
        {
            if (str[i]>='a'&&str[i]<='z')
                res[l++]=str[i];
            else
            {
                res[l]='\0';
                if (mp.find(res)!=mp.end())
                    cout<<mp[res];
                else
                    cout<<res;
                cout<<str[i];
                l=0;
            }
        }
        printf("\n");
    }
}

猜你喜欢

转载自blog.csdn.net/bestfm/article/details/80171657