#include #include #include using namespace std; typedef vector> Input; Input parse() { Input items; for (string line; getline(cin, line);) { vector bag; for (unsigned char c : line) { if (c >= 'a') c = c - 'a' + 1; else if (c >= 'A') c = c - 'A' + 27; bag.push_back(c); } items.push_back(bag); } return items; } template unordered_set intersect(unordered_set &a, unordered_set &b) { unordered_set o; for (auto x : a) if (b.count(x)) o.insert(x); return o; } unsigned p1(Input &items) { unsigned sum = 0; for (auto i : items) { unsigned len = i.size() / 2; auto one = unordered_set(i.begin(), i.begin() + len); auto two = unordered_set(i.begin() + len, i.begin() + len * 2); for (auto x : intersect(one, two)) sum += x; } return sum; } unsigned p2(Input &items) { unsigned sum = 0; for (auto i = items.begin(); i < items.end(); i += 3) { auto one = unordered_set(i[0].begin(), i[0].end()); auto two = unordered_set(i[1].begin(), i[1].end()); auto thr = unordered_set(i[2].begin(), i[2].end()); auto inter = intersect(one, two); inter = intersect(inter, thr); for (auto x : inter) sum += x; } return sum; } int main() { auto input = parse(); cout << p1(input) << endl; cout << p2(input) << endl; }