-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP409.cpp
76 lines (64 loc) · 1.65 KB
/
P409.cpp
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
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <set>
typedef std::string String;
typedef std::set<String> Set;
typedef std::stringstream SS;
typedef std::pair<int,std::string> Excuse;
bool notIsAlpha(char c) {
return !isalpha(c);
}
int badness(const String &excuse, Set keywords, int K) {
int ret = 0;
String copy(excuse);
std::transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
std::replace_if(copy.begin(), copy.end(), notIsAlpha, ' ');
SS ss;
ss << copy;
while(ss >> copy) {
//std::cerr << "Checking '" << copy << "'" << std::endl;
if(keywords.find(copy) != keywords.end())
--ret;
}
//std::cerr << "Excuse '" << excuse << "': " << ret << std::endl;
return ret;
}
String readLine(bool replace) {
char w[309];
gets(w);
if(!replace)
return String(w);
for(int j = 0; true; ++j) {
if(!isprint(w[j])) {
w[j] = '\0';
break;
}
}
return String(w);
}
int main() {
int K, E; // Keywords, excuses.
for(int cas = 1; true; ++cas) {
String s = readLine(false);
SS ss; ss << s;
if(!(ss >> K >> E))
return 0;
Set keywords;
for(int i = 0; i < K; ++i)
keywords.insert(readLine(true));
Excuse *excuses = new Excuse[E];
for(int i = 0; i < E; ++i) {
s = readLine(true);
excuses[i] = Excuse(badness(s, keywords, K), s);
}
std::sort(excuses, excuses+E);
std::cout << "Excuse Set #" << cas << std::endl;
for(int i = 0; i < E && excuses[i].first == excuses[0].first; ++i)
std::cout << excuses[i].second << std::endl;
std::cout << std::endl;
}
}