UVA - 409 - Excuses, Excuses

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


题意:

  输入nKey行关键字,输入nCase行借口,判断哪行借口包含最多关键字。有多个时同时输出。


解题:

  在借口字符串中找关键字,并统计好。注:只有连续的单词才能组成关键字,即"abca ca se"中,只包含一次关键字"ca",而不是两次。

  为了把单词抽出,把所以非字母字符转变成空格,再用sstream把一个个单独单词分离出来,对比即可。

  (开始WA的,后来在厕所想到思路。。。)


#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <string.h>
#include <stdio.h>
using namespace std;

const int MAXN = 20;


void fun_str(string &str)
{
	for ( int i=0; i<str.length(); i++ )
	{
		if ( isalpha(str[i]) )
		{
			str[i] = tolower(str[i]);
		} // end if
		else
		{
			str[i] = ' ';
		} // end else
	} // end for
}


int main()
{
	int nKey, nCase;
	int index = 0;
	
	while ( cin >>nKey >>nCase )
	{
		index++;

		cin.ignore();

		vector <string> vKey;
		vector <string> vExcuses;
		vector <string> vExcusesEx;
		string strIn;

		for ( int i=0; i<nKey; i++ )
		{
			getline(cin, strIn);
			vKey.push_back(strIn);
		} // end for

		for ( int i=0; i<nCase; i++ )
		{
			getline(cin, strIn);
			vExcuses.push_back(strIn);
			fun_str(strIn);
			vExcusesEx.push_back(strIn);
		} // end for

		int szCount[MAXN];
		memset(szCount, 0, sizeof(szCount));
		int max = -1;
		for ( int i=0; i<nCase; i++ )
		{
			istringstream strStream(vExcusesEx[i]);
			string strTmp;
			while ( strStream >>strTmp )
			{
				for ( int j=0; j<nKey; j++ )
				{
					if ( strTmp == vKey[j] )
					{
						szCount[i]++;
					} // end if
				} // end for
			} // end while
			if ( szCount[i] > max )
			{
				max = szCount[i];
			} // end if
		} // end for

		cout <<"Excuse Set #" <<index <<'\n';
		for ( int i=0; i<nCase; i++ )
		{
			if ( szCount[i] == max )
			{
				cout <<vExcuses[i] <<'\n';
			} // end if
		} // end for
		cout <<endl;
		
	} // end while

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值