What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 18989 Accepted Submission(s): 6215
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!HintHuge input, scanf is recommended.
题目大意:
Ignatius很幸运恩,看到了火星人,但是和火星人交流是个问题,因为火星人的文字他是看不懂的;但是呢,这个火星人还是很厉害的,给了他一本神器的字典,这本字典呢,可以将火星文字翻译成他能看得动的英文;火星人还给了他一本关于火星的历史书;为了看懂这些火星文呢,Ignatius需要利用字典去把火星文翻译成英语;
分析:
首先呢我们要知道 火星文只可以通过查字典找出英语的,那么这个‘查’字就很重要了;
然后我们还要知道,如果字典中没有的话,我们只需照搬下来,它就是英语;
现在我们来翻译他,那么我们只要找到其火星文对应的英语,逐个翻译过来就好了,所以这里存在映射关系,我们可以考虑到用 map<>,去处理这件事;,翻译的时候我们还要先确定,该单词是否就是火星文,也就是字典中存不存在该单词,如果不存在,那么它就是英语;
ps: 我这这题上也是错了很多遍的,不是其他的,而是超时;因为map容器中其实有个 find函数,但是我是用自己写的去用,效率上存在比较大的差异吧;所以一直超时,后来我就选择了用kmp去匹配字符串,AC了;这里的话还是用map给大家讲下吧;应为map中有个自带的find函数,那么我们在查找该单词是不是属于字典的时候,其实就可以用这个,效率是比自己写的函数要快的;
给出AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<map>
using namespace std;
int main()
{
char sta[10005], end[10005];
string x, y, str;
map<string, string>mp;
map<string, string>::iterator it;
gets(sta);
int count = 0;
while (cin>>end) //载入字典
{
count++;
if (strcmp(end, "END") == 0)break;
if (count == 1)
{
x += end;
}
if (count == 2)
{
mp[end] = x;
x.clear();
count = 0;
}
}
getchar();
gets(sta);
while (strcmp(sta, "START") != 0)gets(sta);
while (getline(cin,str)) //载入火星人的话
{
if (str == "END")break;
int len = str.length();
for (int i = 0; i < len; i++)
{
if ((str[i] >= 'a'&&str[i] <= 'z') || (str[i] >= 'A'&&str[i] <= 'Z')) //分解每句话中的每个单词;
x += str[i]; //用string x 保存该单词;
else
{
it = mp.find(x); //判断单词是不是字典中的单词;
if (it != mp.end()) //如果是,那么就输出火星文对应的英文;
cout << (*it).second;
else //如果不是,那么就直接输出原文中的单词就好了,这是不需要翻译的;
cout << x;
cout << str[i];
x.clear(); //清空该单词,方便新单词的加载;
}
}
cout << endl;
}
return 0;
}