您的位置:首页 > 其它

UVaOJ 409 - Excuses, Excuses!

2012-11-10 15:46 357 查看
AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume
1. Elementary Problem Solving :: String

Description

无论在公司,还在学校。

为迟早找借口的事常有发生。

你要做的就是,给你一些借口中常常含有的关键词。

找出含有关键词数最多的借口。

注意一下几点:

关键词以小写形式给出,但在借口中,不计大小写。
即使一个借口中有多个关键词相同,它们仍然计算在关键词数内。
关键词在借口中应该连续,且前后必须有“换行”、“空格”或者“非字母字符”。

Type

String

Analysis

将关键词们放在一个set中,

找出每个借口中的单词,判断是否为关键词即可。

Solution

// UVaOJ 409
// Excuses, Excuses!
// by A Code Rabbit

#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;

const int MAXN = 22;

int k, e;
string str;
string excuses[MAXN];
int cnt[MAXN];

int main() {
int cnt_case = 0;
while (cin >> k >> e) {
// Input and solve.
cin.get();
set<string> keywords;
for (int i = 0; i < k; i++) {
getline(cin, str);
keywords.insert(str);
}
memset(cnt, 0, sizeof(cnt));
int max_cnt = 0;
for (int i = 0; i < e; i++) {
getline(cin, excuses[i]);
str = excuses[i];
string tmp;
for (int j = 0; j < str.length(); j++) {
if (!isalpha(str[j])) {
if (keywords.count(tmp)) cnt[i]++;
tmp = "";
} else {
tmp += tolower(str[j]);
}
}
max_cnt = max(max_cnt, cnt[i]);
}
// Output.
cout << "Excuse Set #" << ++cnt_case << endl;
for (int i = 0; i < e; i++)
if (cnt[i] == max_cnt)
cout << excuses[i] << endl;
cout << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: