What Are You Talking About-HDUOJ

What Are You Talking About


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!

思路:这道题很明显是字符串对应替换问题,在JAVA中用Hashmap,这里C++用map进行一一对应。但是,由于原来字符串并不是用空格\space进行一一分隔的,有的地方还会出现,所以不能用读取一个处理一个的方法进行。现在就有以下两个方向:

  • 从字典出发,依次查找字符串中是否有与字典相对应的,然后进行替换
  • 从字符串出发,查找字典中是否有相对应的,进行替换

如果从字典出发(遍历字典),时间会超限。所以,我们选择从字符串出发。

AC代码

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main() {
	// 字典
	map<string, string> m;
	// 标志:"START" & "END"
	string sign;
	string first, second;

	cin >> sign;

	// 制作字典
	while (cin >> first) {
		if (first == "END") {
			break;
		}
		cin >> second;
		m.insert(pair<string,string>(second, first) );
	}

	cin >> sign;
	string content;

	// 吸收回车
	getchar();
	while (getline(cin,content) ) {
		if (content == "END") {
			break;
		}
		// 加一个'.'防止最后是没有句号,导致最后一个单词本来要替换,结果直接跳出了
		content += ".";
		// 用来存取截取的部分字符串
		string tmp = "";
		int tmp_len = 0;
		// 注意这里content的长度是变化的,所以要用size()函数
		for (int i = 0; i < content.size(); i++) {
			if (isalpha(content[i])) {
				tmp_len++;
				tmp += content[i];
			}
			else {
				map<string, string>::iterator it = m.find(tmp);
				if (it != m.end()) {
					content.replace(i - tmp_len, tmp_len, it->second);
					i += it->second.size() - tmp_len;
				}
				tmp_len = 0;
				tmp = "";
			}
		}
		int _len = content.size();
		// 最后一个'.'不要输出
		for (int i = 0; i < _len - 1; i++) {
			cout << content[i];
		}
		cout << endl;
	}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

中小庸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值