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?
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END
hello, i'm from mars.
i like earth!
Huge input, scanf is recommended.
题意:
首先输入每行两个字符串,第一个是翻译的语言,第二个数被翻译的语言。
接下来输入几行话,若上面有此词的翻译则翻译,没有则原样输出。字典树和map都可以写
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<sstream>
#include<map>
#include<algorithm>
using namespace std;
string s;
int main(){
cin>>s;
map<string,string>k;
while(getline(cin,s)){
if(s=="END")break;
string a="",b="";
int f=1;
for(int i=0;i<s.size();i++){
if(s[i]==' '){
f=0;
continue;
}
if(f){
a+= s[i];
}else{
b+=s[i];
}
}
k[b]=a;
}
cin>>s;
getchar();
while(getline(cin,s)){
if(s=="END")break;
int len = s.length();
s+='\n';//很重要
s+='\0';
string p="";
int f=0;
for(int i=0;i<=len;i++){
while(s[i]>='a'&&s[i]<='z'){
p+=s[i];
f=1;
i++;
}
if(f){
if(k[p]=="")printf("%s",p.c_str());
else printf("%s",k[p].c_str());
//string 类型的输出形式,输入同理
f=0;
i--;
continue;
}
p="";
printf("%c",s[i]);
}
}
return 0;
}