您的当前位置:首页正文

uva 409 poj 1598

2024-11-25 来源:个人技术集锦

uva 409和poj 1598是同一道题。这道题也算是基础题了,主要有两点需要注意

1. A keyword "occurs" in an excuse if and only if it exists in the string in contiguous form and is delimited by the beginning or end of the line or any non-alphabetic character or a space. 所以说并不是简单地在一个字符串中寻找特定的子串,这个子串的前后需要是句子的开始或结束或是符合要求的分隔符。

2. 题目中说明了可能出现符号{.,!?}, 但是我第一次用这些符号做匹配时wa了,之后就把字符串中所有不是字母的字符全换为空格,这样更好处理

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <memory>

using namespace std;

int knum, enumber;
const int maxn = 20 + 10;
string keywords[maxn], execuses[maxn], ori[maxn];
int cnt[maxn];

void read()
{
	for (int i = 0; i < knum; i++)
		cin >> keywords[i];
	getchar();
	for (int i = 0; i < enumber; i++)
	{
		getline(cin, execuses[i]);
		ori[i] = execuses[i];
	}
	for (int i = 0; i < enumber; i++)
	{
		for (int j = 0; j < execuses[i].length(); j++)
		{
			if (isalpha(execuses[i][j]))
				execuses[i][j] = tolower(execuses[i][j]);
			else
				execuses[i][j] = ' ';
		}
	}
}

bool validSep(char ch)
{
	if (ch == ' ' || ',' == ch || '.' == ch || '?' == ch || '!' == ch)
		return true;
	return false;
}

void search()
{
	for (int i = 0; i < enumber; i++)
	{
		for (int j = 0; j < knum; j++)
		{
			int start = 0, pos = 0;
			while (true)
			{
				pos = execuses[i].find(keywords[j], start);
				if (pos == string::npos)
					break;
				int endPos = pos + keywords[j].length();
				if (endPos >= execuses[i].length() || validSep(execuses[i][endPos]))
				{
					if (0 == start || validSep(execuses[i][start - 1]))
						cnt[i]++;
				}
;
				start = pos + keywords[j].length();
			}
		}
	}//for enumber
}

int main()
{
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	int kase = 0;
	while (cin >> knum >> enumber)
	{
		read();
		memset(cnt, 0, sizeof(cnt));
		search();

		int cntMax = -1;
		for (int i = 0; i < enumber; i++)
		{
			if (cntMax < cnt[i])
				cntMax = cnt[i];
		}
		
		cout << "Excuse Set #" << ++kase << endl;
		for (int i = 0; i < enumber; i++)
		{
			if (cntMax == cnt[i])
				cout << ori[i] << endl;
		}
		cout << endl;
	}//while

	return 0;
}


显示全文