UVa Problem 123 - Searching Quickly

 
// UVa Problem 123 - Searching Quickly 
// Verdict: Accepted
// Submission Date: 2012-01-04
// UVa Run Time: 0.016s
//
// 版权所有(C)2012,邱秋。metaphysis # yeah dot net
//
// [解题方法]
// 简单的字符串处理和排序题。
//
// 提交时错误了一次,因为没有注意到需要忽略的关键词可能会重复给出的情况,修正后就 AC 了。

#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>
#include <set>

using namespace std;

struct title
{
	string content;
	string keyword;
	int indexKeyword;
	int indexInput;
};

string toLower(string s)
{
	string r;
	for (int i = 0; i < s.length(); i++)
		r += (s[i] >= 'A' && s[i] <= 'Z' ? (s[i] + 32) : s[i]);
	return r;
}

string toUpper(string s)
{
	string r;
	for (int i = 0; i < s.length(); i++)
		r += (s[i] >= 'a' && s[i] <= 'z' ? (s[i] - 32) : s[i]);
	return r;
}

bool cmp(title a, title b)
{
	if (a.keyword == b.keyword)
	{
		if (toLower(a.content) == toLower(b.content))
			return a.indexKeyword < b.indexKeyword;
		else
			return a.indexInput < b.indexInput;
	}

	return a.keyword < b.keyword;
}

int main (int argc, char const* argv[])
{
	set < string > ignore;
	vector < title > titles;
	string line;
	
	while (getline(cin, line))
	{
		if (line != "::")
		{
			// 可能会给出重复的关键词,需要消除。
			if (ignore.find(line) == ignore.end())
				ignore.insert(line);
		}
		else
		{
			int count = 0;
			while (getline(cin, line))
			{
				string all = toLower(line);

				int i = 0;
				while (i < line.length())
				{
					if (line[i] == ' ')
						i++;
					else
					{
						// 将句子中出现的单词逐个分离出来。
						int start = i;
						string tmp;
						while (i < line.length() && line[i] != ' ')
						{
							tmp += line[i];
							i++;
						}
						int end = i;

						// 若不是需要忽略的关键词,则生成一个标题。
						tmp = toLower(tmp);
						if (ignore.find(tmp) == ignore.end())
						{
							string keyword = toUpper(tmp);
							string content = all.substr(0, start) + keyword + all.substr(end);
							title t = (title) {content, keyword, start, count};
							titles.push_back(t);
						}
					}
				}
				
				count++;
			}
		}	
	}

	// 按指定规则排序。
	sort(titles.begin(), titles.end(), cmp);
	
	for (int i = 0; i < titles.size(); i++)
		cout << titles[i].content << endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值