409.cpp

Back Download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 409.cpp http://acm.uva.es/p/v4/409.html
//
// Solution to ACM problem #409 - "Excuses, Excuses!"
//
// Matthew Eagar - meagar@gmail.com
// Verified to work as of Sept 21 / 2006
#include <iostream>
#include <string>
#include <cctype>
#include <cstring>
#include <vector>
int main() {
int numKeys, numEx, n = 0;
typedef std::vector<std::string> Vec;
while (std::cin >> numKeys >> numEx) {
++n;
std::cin.get();
Vec keywords(numKeys);
for (Vec::iterator it = keywords.begin(); it != keywords.end(); ++it)
std::getline(std::cin, *it);
Vec excuses(numEx);
for (Vec::iterator it = excuses.begin(); it != excuses.end(); ++it)
std::getline(std::cin, *it);
int max = 0;
std::vector<int> keyCount(numEx);
for (int i = 0; i < numEx; ++i) {
std::string tmp = excuses[i];
for (unsigned int j = 0; j < tmp.length(); ++j)
tmp[j] = std::tolower(tmp[j]);
for (int j = 0; j < numKeys; ++j) {
int p = -1;
while ((p = (int)tmp.find(keywords[j], p + 1)) != (int)tmp.npos) {
// check for leading delimeter
if (p >= 1 && isalpha(tmp[p - 1]))
continue;
// check for tailing delimeter
int n = p + keywords[j].length();
if (n <= (int)tmp.length() && isalpha(tmp[n]))
continue;
++(keyCount[i]);
}
}
if (keyCount[i] > max)
max = keyCount[i];
}
std::cout << "Excuse Set #" << n << std::endl;
for (int i = 0; i < numEx; ++i)
if (keyCount[i] == max)
std::cout << excuses[i] << std::endl;
std::cout << std::endl;
}
}