/* 2021 * Maciej Szeptuch */ #include <cstdint> #include <cstdio> #include <functional> #include <map> #include <unordered_map> #include <utility> #include <vector> const int MAX_LENGTH = 524288; char word[MAX_LENGTH]; int as; int bs; int cs; long long int count; struct hash_pair { size_t operator()(const std::pair<int, int> &p) const { auto first = std::hash<int>{}(p.first); auto second = std::hash<int>{}(p.second); return first ^ (second + 0x9e3779b9 + (first << 6) + (first >> 2)); } }; // typedef std::map<std::pair<int, int>, int> Map; typedef std::unordered_map<std::pair<int, int>, int, hash_pair> Map; Map abc; // a == b == c Map ab; // a == b && c == 0 Map ac; // a == c && b == 0 Map bc; // b == c && a == 0 Map a; // b == c == 0 Map b; // a == c == 0 Map c; // a == b == 0 int main(void) { scanf("%s", word); abc[{0, 0}] = 1; ab[{0, 0}] = 1; ac[{0, 0}] = 1; bc[{0, 0}] = 1; a[{0, 0}] = 1; b[{0, 0}] = 1; c[{0, 0}] = 1; for(int w = 1; word[w - 1]; ++w) { as += (word[w - 1] == 'a'); bs += (word[w - 1] == 'b'); cs += (word[w - 1] == 'c'); auto _abc = std::make_pair(as - bs, as - cs); auto _ab = std::make_pair(cs, as - bs); auto _ac = std::make_pair(bs, as - cs); auto _bc = std::make_pair(as, bs - cs); auto _a = std::make_pair(bs, cs); auto _b = std::make_pair(as, cs); auto _c = std::make_pair(as, bs); count += abc[_abc] + ab[_ab] + ac[_ac] + bc[_bc] + a[_a] + b[_b] + c[_c]; abc[_abc] += 1; ab[_ab] += 1; ac[_ac] += 1; bc[_bc] += 1; a[_a] += 1; b[_b] += 1; c[_c] += 1; } printf("%lld\n", count); return 0; }
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 77 78 | /* 2021 * Maciej Szeptuch */ #include <cstdint> #include <cstdio> #include <functional> #include <map> #include <unordered_map> #include <utility> #include <vector> const int MAX_LENGTH = 524288; char word[MAX_LENGTH]; int as; int bs; int cs; long long int count; struct hash_pair { size_t operator()(const std::pair<int, int> &p) const { auto first = std::hash<int>{}(p.first); auto second = std::hash<int>{}(p.second); return first ^ (second + 0x9e3779b9 + (first << 6) + (first >> 2)); } }; // typedef std::map<std::pair<int, int>, int> Map; typedef std::unordered_map<std::pair<int, int>, int, hash_pair> Map; Map abc; // a == b == c Map ab; // a == b && c == 0 Map ac; // a == c && b == 0 Map bc; // b == c && a == 0 Map a; // b == c == 0 Map b; // a == c == 0 Map c; // a == b == 0 int main(void) { scanf("%s", word); abc[{0, 0}] = 1; ab[{0, 0}] = 1; ac[{0, 0}] = 1; bc[{0, 0}] = 1; a[{0, 0}] = 1; b[{0, 0}] = 1; c[{0, 0}] = 1; for(int w = 1; word[w - 1]; ++w) { as += (word[w - 1] == 'a'); bs += (word[w - 1] == 'b'); cs += (word[w - 1] == 'c'); auto _abc = std::make_pair(as - bs, as - cs); auto _ab = std::make_pair(cs, as - bs); auto _ac = std::make_pair(bs, as - cs); auto _bc = std::make_pair(as, bs - cs); auto _a = std::make_pair(bs, cs); auto _b = std::make_pair(as, cs); auto _c = std::make_pair(as, bs); count += abc[_abc] + ab[_ab] + ac[_ac] + bc[_bc] + a[_a] + b[_b] + c[_c]; abc[_abc] += 1; ab[_ab] += 1; ac[_ac] += 1; bc[_bc] += 1; a[_a] += 1; b[_b] += 1; c[_c] += 1; } printf("%lld\n", count); return 0; } |