#include <iostream> #include <unordered_map> struct pair_hash { template<class T1, class T2> std::size_t operator()(const std::pair<T1, T2> &p) const { auto h1 = std::hash<T1>{}(p.first); auto h2 = std::hash<T2>{}(p.second); return h1 ^ h2; } }; uint64_t triangle(uint64_t n) { uint64_t count = 0; while (n) count += n--; return count; } uint64_t do_abc(const std::string &str) { size_t size = str.size(); std::unordered_map<std::pair<int, int>, int, pair_hash> map; map[std::make_pair(0, 0)] = 1; for (int i = 0, a = 0, b = 0; i < size; i++) { // std::cout << i << std::endl; switch (str[i]) { case 'a': ++a; break; case 'b': ++b; break; case 'c': --a; --b; break; } auto key = std::make_pair(a, b); if (map.find(key) == map.end()) { map[key] = 1; } else { map[key]++; } } uint64_t count = 0; for (auto &p: map) { // std::cout << "Processing " << p.first.first << ", " << p.first.second << std::endl; int n = p.second; while (n--) count += n; } return count; } uint64_t do_two(const std::string &str, char c1, char c2) { size_t size = str.size(); std::unordered_map<int, int> map; map[0] = 1; uint64_t count = 0; for (int i = 0, a = 0; i < size; i++) { if (str[i] == c1) { ++a; } else if (str[i] == c2) { --a; } else { for (auto &p: map) { // std :: cout << p.first << ": " << p.second << std::endl; uint64_t n = p.second; while (n--) count += n; } map.clear(); map[0] = 1; a = 0; continue; } if (map.find(a) == map.end()) { map[a] = 1; } else { map[a]++; } } for (auto &p: map) { // std :: cout << p.first << ": " << p.second << std::endl; uint64_t n = p.second; while (n--) count += n; } return count; } uint64_t do_one(const std::string &str, char c) { size_t size = str.size(); uint64_t count = 0; uint64_t streak = 0; for (uint64_t i = 0; i < size; i++) { if (str[i] == c) { ++streak; // std::cout << "I see a '" << str[i] << "', streak: " << streak << std::endl; } else { // std::cout << "I see a '" << str[i] << "', streak: " << streak << std::endl; count += triangle(streak); streak = 0; // std::cout << "Count: " << count << std::endl; } } return count + triangle(streak); } int main() { std::string line; std::getline(std::cin, line); uint64_t count = do_abc(line); // std::cout << "After including " << "abc" << ": " << count << std::endl; count += do_two(line, 'a', 'b'); // std::cout << "After including " << "ab" << ": " << count << std::endl; count += do_two(line, 'b', 'c'); // std::cout << "After including " << "bc" << ": " << count << std::endl; count += do_two(line, 'a', 'c'); // std::cout << "After including " << "ac" << ": " << count << std::endl; count += do_one(line, 'a'); // std::cout << "After including " << "a" << ": " << count << std::endl; count += do_one(line, 'b'); // std::cout << "After including " << "b" << ": " << count << std::endl; count += do_one(line, 'c'); // std::cout << "After including " << "c" << ": " << count << std::endl; std::cout << count << std::endl; 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #include <iostream> #include <unordered_map> struct pair_hash { template<class T1, class T2> std::size_t operator()(const std::pair<T1, T2> &p) const { auto h1 = std::hash<T1>{}(p.first); auto h2 = std::hash<T2>{}(p.second); return h1 ^ h2; } }; uint64_t triangle(uint64_t n) { uint64_t count = 0; while (n) count += n--; return count; } uint64_t do_abc(const std::string &str) { size_t size = str.size(); std::unordered_map<std::pair<int, int>, int, pair_hash> map; map[std::make_pair(0, 0)] = 1; for (int i = 0, a = 0, b = 0; i < size; i++) { // std::cout << i << std::endl; switch (str[i]) { case 'a': ++a; break; case 'b': ++b; break; case 'c': --a; --b; break; } auto key = std::make_pair(a, b); if (map.find(key) == map.end()) { map[key] = 1; } else { map[key]++; } } uint64_t count = 0; for (auto &p: map) { // std::cout << "Processing " << p.first.first << ", " << p.first.second << std::endl; int n = p.second; while (n--) count += n; } return count; } uint64_t do_two(const std::string &str, char c1, char c2) { size_t size = str.size(); std::unordered_map<int, int> map; map[0] = 1; uint64_t count = 0; for (int i = 0, a = 0; i < size; i++) { if (str[i] == c1) { ++a; } else if (str[i] == c2) { --a; } else { for (auto &p: map) { // std :: cout << p.first << ": " << p.second << std::endl; uint64_t n = p.second; while (n--) count += n; } map.clear(); map[0] = 1; a = 0; continue; } if (map.find(a) == map.end()) { map[a] = 1; } else { map[a]++; } } for (auto &p: map) { // std :: cout << p.first << ": " << p.second << std::endl; uint64_t n = p.second; while (n--) count += n; } return count; } uint64_t do_one(const std::string &str, char c) { size_t size = str.size(); uint64_t count = 0; uint64_t streak = 0; for (uint64_t i = 0; i < size; i++) { if (str[i] == c) { ++streak; // std::cout << "I see a '" << str[i] << "', streak: " << streak << std::endl; } else { // std::cout << "I see a '" << str[i] << "', streak: " << streak << std::endl; count += triangle(streak); streak = 0; // std::cout << "Count: " << count << std::endl; } } return count + triangle(streak); } int main() { std::string line; std::getline(std::cin, line); uint64_t count = do_abc(line); // std::cout << "After including " << "abc" << ": " << count << std::endl; count += do_two(line, 'a', 'b'); // std::cout << "After including " << "ab" << ": " << count << std::endl; count += do_two(line, 'b', 'c'); // std::cout << "After including " << "bc" << ": " << count << std::endl; count += do_two(line, 'a', 'c'); // std::cout << "After including " << "ac" << ": " << count << std::endl; count += do_one(line, 'a'); // std::cout << "After including " << "a" << ": " << count << std::endl; count += do_one(line, 'b'); // std::cout << "After including " << "b" << ": " << count << std::endl; count += do_one(line, 'c'); // std::cout << "After including " << "c" << ": " << count << std::endl; std::cout << count << std::endl; return 0; } |