#include #include #include #include using namespace std; int getPriority(char c) { // [1;52] if ('a' <= c && c <= 'z') return c - 'a' + 1; if ('A' <= c && c <= 'Z') return c - 'A' + 27; throw "unexpected input"; } int main() { long total1 = 0; long total2 = 0; string line; // don't reallocate while (!cin.eof()) { uint8_t masks[52] = {0}; for (int i = 0; i < 3; i++) { bool inFirstHalf[52] = {0}; int found = -1; if (!getline(cin, line)) break; if (line.length() % 2 != 0) throw "unexpected input"; for (int j = 0; j < line.length(); j++) { int p = getPriority(line[j]); masks[p-1] = masks[p-1] | (1 << i); if (j < line.length() / 2) { inFirstHalf[p-1] = true; } else { if (inFirstHalf[p-1]) { if (found != -1 && found != p) throw "bad input"; found = p; } } } if (found == -1) throw "bad input"; total1 += found; } if (!cin.eof()) { if (count(begin(masks), end(masks), 7) != 1) throw "bad input"; total2 += find(begin(masks), end(masks), 7) - begin(masks) + 1; } } cout << total1 << endl; cout << total2 << endl; }