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;
}