您的位置:首页 > 其它

UVa 409 - Excuses, Excuses!

2014-06-13 01:19 375 查看
题目:有一些关键词,还有一些说明理由的句子。输出关键词出现最多次数的句子,若有多个都输出。

分析:字符串。把句子拆成单词,依次和所有关键词比较即可。

处理时注意,必须是关键词匹配,不能使前缀。

说明:同以关键词多次出现,按多次计算;每组结束都有换行。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

char keywords[24][24];
char excuses[24][80];
int  counts[24];

int small( char a )
{
	if ( a >= 'A' && a <= 'Z' )
		return a +'a'-'A';
	else return a;
}

int find_word( char *a, char *b )
{
	char buf[80];
	int  count = 0,move = 0;
	while ( a[move] ) {
		while ( a[move] && (small(a[move]) < 'a' || small(a[move]) > 'z') )
			move ++;
		int save = 0;
		while ( small(a[move]) >= 'a' && small(a[move]) <= 'z' )
			buf[save ++] = small(a[move ++]);
		buf[save] = 0;
		count += !strcmp(b,buf);
	}
	return count;
}

int main()
{
	int n,m,T = 1;
	while ( ~scanf("%d%d",&n,&m) ) {
		//getchar();
		for ( int i = 0 ; i < n ; ++ i ) {
			scanf("%s",keywords[i]);
			getchar();
		}
		for ( int i = 0 ; i < m ; ++ i ) {
			gets(excuses[i]);
			counts[i] = 0;
		}
		
		int max = 0;
		for ( int i = 0 ; i < m ; ++ i ) {
			for ( int j = 0 ; j < n ; ++ j )
				counts[i] += find_word( excuses[i], keywords[j] );
			if ( max < counts[i] )
				max = counts[i];
		}
		
		printf("Excuse Set #%d\n",T ++);
		for ( int i = 0 ; i < m ; ++ i )
			if ( counts[i] == max )
				printf("%s\n",excuses[i]);
		printf("\n");
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: