UVA - 123 - Searching Quickly

8 篇文章 0 订阅

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=59


题意:

    输入一些“非关键字”,再输入一些行字符串,要求找出所有关键字后,对行字符串进行排序输出。排序原则:第一优先,关键字升序;第二优先,行字符串升序。

    一个行字符串可能出现多个不同的关键字,如题目要求,没关系;一个行字符串可能多次出现同一个关键字,这个行字符串,以关键字的出现位置为序,输出多次。


解题:

    首先使用set保存“非关键字”。set的值唯一性,不会出现重复;而且set可以直接查询“值”是否包含在set里,使用方法set.count(值),方便。而vector没有这样的功能。

    接着保存输入的每一行字符串,输入一行处理一行。

    使用最基本的方向从行字符串中分离出单词。对每个字符进行判断,以确定什么时候是单词的开始,然后一个一个保存到string里。

    处理时,先使用set.count()判断该单词是否在“非关键字”集合里;如否,则为关键字,使用stl::transform转换成大写,并直接写入行字符串中,方便。

    最后把每个pair(关键字,对应处理完的行字符串)保存到multimap里。multimap支持一对多的数学关系,而map则是一对一。

    输出结果。


注意:

    1. 本来使用的是vector来保存“非关键字”,可判断关键字时,就麻烦了点。优先使用set。

    2. 原使用map保存结果,可一对一,string to string2,只能在string2里追加'\n'+string,很投机的方法,如果客户要求改变输出结果,那整个process都得去研究,所以要求平常写函数输出/保存结果时,不要把格式也保存进去,如保存回车等之类的字符在里面,那后期很难再使用/难维护。

    3. 一贯使用istringstream来分离行字符串的,可分离后,没有了index等数据,后续不好操作。

    4. stl::transform是个好东西。 

    5. 看别人的题解往往有好收获。。。


#include <iostream>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <vector>
#include <stdio.h>
using namespace std;

// #define LOCAL_TEST

int main()
{
#ifdef LOCAL_TEST
	freopen("f:\\in.txt", "r", stdin);
	freopen("f:\\out.txt", "w+", stdout);
#endif
	// used to store words to ignore,
	// "SET" is a Simple Associative Container, meaning that its value
	// type, as well as its key type, is Key. It is also a Unique
	// Associative Container meaning that no two elements are the same.
	set<string> sIgnore;
	// used to store Results
	// "Multimap" is a Sorted Associative Container that associates
	// objects of type Key with objects of type Data. "Multimap" is a Pair
	// Associative Container, meaning that its value type is 
	// pair<const Key, Data>. It is also a Multiple Associative 
	// Container, meaning that there is no limint on the number of
	// elements with the same key.
	multimap<string, string> mRes;

	// user char* to input lineData, just have to use "gets()"
	char strIn[1000];
	while ( gets(strIn) )
	{
		// while input "::", break
		if ( strIn[0]==':' && strIn[1]==':' )
			break;
		// use string&STL to lowercase
		string str(strIn);
		transform(str.begin(), str.end(), str.begin(), ::tolower);
		// store to sIgnore(set)
		sIgnore.insert(str);
	} // end while

	// gets titles, and processing
	while ( gets(strIn) )
	{
		// lowercase title
		string strList(strIn);
		transform(strList.begin(), strList.end(), strList.begin(), ::tolower);
		// processing
		for ( int i=0; i<strList.length(); i++ )
		{
			if ( !isalpha(strList[i]) )
				continue;
			// meaning inputing a new words in title
			// separate and get the current word
			string strWord;
			int k = i;
			while ( isalpha(strList[k]) )
			{
				strWord += strList[k];
				k++;
			} // end while
			// sIgnore not contain current word, meaning current one
			// is key word
			if ( !sIgnore.count(strWord) )
			{	// copy the strList to a temporary string (strTmp)
				string strTmp = strList;
				// uppercase to and save the title
				transform(strWord.begin(), strWord.end(),
					strTmp.begin()+i, ::toupper);
				// store to multimap
				mRes.insert(make_pair(strWord, strTmp));
			} // end if
			// index change (step to k)
			i = k;
		} // end for
	} // end while

	// print out
	for ( multimap<string, string>::iterator it = mRes.begin();
		it != mRes.end(); it++)
		cout <<it->second <<'\n';
	
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值