1.题目引入:
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.
2.样例输出:
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.
题目大意: 将给定的一些字符串转译成所需要的字符串,如果没有所对应的字符串则原样输出即可。
本题使用 map容器和 string 容器显然更好,这里需要注意:因为输入时涉及到空格和回车,因此仅仅使用 cin 输入是不行的,这里给出三种输入方式:
char a[10]; cin.getline(a,10);
char a[10]; cin.read(a,10); cout.write(a,10); //只能读取指定字符长度且可以读取空格
string a; getline(cin,a);
除此之外还需要注意每单个字符串赋值给其他字符串时,需要将赋值的字符串改为如:
string a=""; 即可。
如果为单个字符一个个的赋给另一字符串 则 应写为 string a; a=a+...。
3.代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
int main()
{
string a,b;
map<string,string> m;
getline(cin,a); //字符串输入
while(cin>>a)
{
if(a=="END")
{
break;
}
else
{
cin>>b;
m[b]=a;
}
}
getline(cin,a); //吸收回车字符
getline(cin,a);
string s1=""; //必须要写为 s1="";
while(1)
{
getline(cin,a);
if(a=="END") break;
else
{
s1=s1+a+"\n"; //输出最后的回车键
}
}
string temp="";
for(int i=0;i<s1.length();i++)
{
if('a'<=s1[i]&&s1[i]<='z')
//除了26个字符外其他原样输出,这自然包含空格和回车键
{
temp+=s1[i];
}
else
{
map<string,string>::iterator it=m.find(temp);
if(it!=m.end())
{
cout<<it->second;
}
else
{
cout<<temp;
}
temp="";
cout<<s1[i];
}
}
return 0;
}