您的位置:首页 > 其它

HDU-1075-What Are You Talking About

2017-03-19 11:16 295 查看

What Are You Talking About

[b]Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)

Total Submission(s): 21918    Accepted Submission(s): 7305
[/b]

[align=left]Problem Description[/align]
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?

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
In this problem, you have to output the translation of the history book.

 

[align=left]Sample Input[/align]

START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END

 

[align=left]Sample Output[/align]

hello, i'm from mars.
i like earth!

Hint
Huge input, scanf is recommended.

 

[align=left]Author[/align]
Ignatius.L
 

题意是在两个不同的语言之间翻译 先输入字典中英语与火星语的对应关系 以START和END对应开始和结束 后面的在来一对
START和END 其中的数据表示要翻译的火星语
翻译的过程由如下可能
1 如果字典中有 就直接翻译
2 如果字典中没有 就照抄
输出翻译后的字符串

本题可以使用map和string类搞  利用map的count方法查找是否有这个词
按行读取 分析每一个字符 将他append到一个string word中 读到非字符数据 就把word append到结果string中去
每行结束后在append一个换行符即可注意每次word被append 到结果string后要清空

ac code
#include<bits/stdc++.h>
using namespace std;
char c[3030];
int main()
{
map<string ,string>m;
map<string,string>::iterator it;
string a;
getline(cin,a);
if(a=="START")
{
getline(cin,a);
while(a!="END")
{
string s,e;
int pos = a.find(' ',0);//从第二个参数的位置开始查找到第一个参数的字符位置返回回去
for(int i=0;i!=pos;i++)s.append(1,a[i]);//在s的末尾添加上 第一个参数表示数量 个 第二个参数字符
for(int i=pos+1;i!=a.length();i++)e.append(1,a[i]);
//	cout<<"show:"<<s<<" "<<e<<endl;
m[e]=s;
getline(cin,a);
}
getline(cin,a);
string res,word;
if(a=="START")
{
getline(cin,a);
while(a!="END")
{
for(int i=0;i<a.length();i++)
{
//	cout<<word<<endl;
if(!isalpha(a[i]))
{
if(m.count(word))res.append(m[word]);
else res.append(word);
res.append(1,a[i]);
word.assign("");
}
else word.append(1,a[i]);
}
res.append("\n");
getline(cin,a);
}
cout<<res;
}

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: