#include <cstdio> #include <cstdlib> #include <cstdint> #include <tuple> #include <map> static const size_t MAX_N = 300 * 1000; char word[MAX_N + 1]; long long int count_singular(size_t N) { long long int ret = 0; char prev = 'x'; size_t last_pos = 0; for (size_t i = 0; i < N; i++) { if (prev != word[i]) { prev = word[i]; last_pos = i; } ret += i + 1 - last_pos; } return ret; } long long int count_double(size_t N, char c1, char c2) { long long int ret = 0; std::map<int64_t, long long int> tracker; tracker[0] = 1; int64_t bias = 0; for (size_t i = 0; i < N; i++) { if (word[i] == c1) { ++bias; } else if (word[i] == c2) { --bias; } else { tracker.clear(); tracker[0] = 1; bias = 0; continue; } ret += tracker[bias]++; } return ret; } long long int count_triple(size_t N) { long long int ret = 0; std::map<int64_t, long long int> tracker; int64_t bias = 0; tracker[bias] = 1; for (size_t i = 0; i < N; i++) { if (word[i] == 'a') { bias -= 1 << 20; bias -= 1; } else if (word[i] == 'b') { bias += 2 << 20; bias -= 1; } else { bias -= 1 << 20; bias += 2; } ret += tracker[bias]++; } return ret; } int main() { size_t N = 0; if (!fgets(word, MAX_N+1, stdin)) { return 1; } while (true) { if (word[N] != 'a' && word[N] != 'b' && word[N] != 'c') { break; } N++; } long long int ret = 0; ret += count_singular(N); ret += count_double(N, 'a', 'b'); ret += count_double(N, 'a', 'c'); ret += count_double(N, 'b', 'c'); ret += count_triple(N); printf("%lld\n", ret); 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 | #include <cstdio> #include <cstdlib> #include <cstdint> #include <tuple> #include <map> static const size_t MAX_N = 300 * 1000; char word[MAX_N + 1]; long long int count_singular(size_t N) { long long int ret = 0; char prev = 'x'; size_t last_pos = 0; for (size_t i = 0; i < N; i++) { if (prev != word[i]) { prev = word[i]; last_pos = i; } ret += i + 1 - last_pos; } return ret; } long long int count_double(size_t N, char c1, char c2) { long long int ret = 0; std::map<int64_t, long long int> tracker; tracker[0] = 1; int64_t bias = 0; for (size_t i = 0; i < N; i++) { if (word[i] == c1) { ++bias; } else if (word[i] == c2) { --bias; } else { tracker.clear(); tracker[0] = 1; bias = 0; continue; } ret += tracker[bias]++; } return ret; } long long int count_triple(size_t N) { long long int ret = 0; std::map<int64_t, long long int> tracker; int64_t bias = 0; tracker[bias] = 1; for (size_t i = 0; i < N; i++) { if (word[i] == 'a') { bias -= 1 << 20; bias -= 1; } else if (word[i] == 'b') { bias += 2 << 20; bias -= 1; } else { bias -= 1 << 20; bias += 2; } ret += tracker[bias]++; } return ret; } int main() { size_t N = 0; if (!fgets(word, MAX_N+1, stdin)) { return 1; } while (true) { if (word[N] != 'a' && word[N] != 'b' && word[N] != 'c') { break; } N++; } long long int ret = 0; ret += count_singular(N); ret += count_double(N, 'a', 'b'); ret += count_double(N, 'a', 'c'); ret += count_double(N, 'b', 'c'); ret += count_triple(N); printf("%lld\n", ret); return 0; } |