-
Notifications
You must be signed in to change notification settings - Fork 0
/
laundry.cc
43 lines (38 loc) · 1.15 KB
/
laundry.cc
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
#include <algorithm>
#include <iostream>
#include <string>
#include <set>
int main() {
const auto printItem = [](const std::size_t count, const std::string& item) {
std::cout << count << '|' << item << '\n';
};
const auto alpha_compare =
[](const std::string& lhs, const std::string& rhs) {
return std::lexicographical_compare(
std::begin(lhs), std::end(lhs), std::begin(rhs), std::end(rhs),
[](char l, char r) { return std::tolower(l) < std::tolower(r); });
};
auto items =
std::multiset<std::string, decltype(alpha_compare)>(alpha_compare);
auto line = std::string{};
while (getline(std::cin, line)) {
if (line != "") {
items.emplace(line);
}
}
for (auto it = std::begin(items); it != std::end(items);
it = items.upper_bound(*it)) {
const auto& item = *it;
auto count = items.count(item);
if (item.find("sock") != std::string::npos) { // we have a sock!
if (count > 1) {
printItem(count / 2, item);
}
if (count % 2 == 1) { // one lonely sock left over
printItem(0, item);
}
} else {
printItem(count, item);
}
}
}