#include <iostream> #include <map> #include <string> #include <cstdint> std::string s; int64_t total; void countOneLetters(){ char last = s[0]; int64_t subseqSize = 0; for(char c: s){ if(c == last){ subseqSize++; } else { total += subseqSize * (subseqSize + 1) / 2; subseqSize = 1; last = c; } } total += subseqSize * (subseqSize + 1) / 2; } void countTwoLetters(){ struct Counter{ std::map<int, int64_t> diffs; int currDiff = 0; Counter(){ diffs[0] = 1; } void update(int d){ currDiff += d; diffs[currDiff]++; } void reset(){ for(auto [diff, points]: diffs){ total += points * (points - 1) / 2; } currDiff = 0; diffs.clear(); diffs[0] = 1; } } counters[3]; for(char c: s){ int i = c - 'a'; counters[i].update(1); counters[(i+1)%3].update(-1); counters[(i+2)%3].reset(); } counters[0].reset(); counters[1].reset(); counters[2].reset(); } void countThreeLetters(){ std::map<std::pair<int, int>, int64_t> diffs; std::pair<int, int64_t> currDiff = {0, 0}; diffs[currDiff] = 1; for(char c: s){ if(c == 'a'){ currDiff.first++; } else if(c == 'b'){ currDiff.second++; } else { currDiff.first--; currDiff.second--; } diffs[currDiff]++; } for(auto [diff, points]: diffs){ total += points * (points - 1) / 2; } } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cin >> s; countOneLetters(); countTwoLetters(); countThreeLetters(); std::cout << total; 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 | #include <iostream> #include <map> #include <string> #include <cstdint> std::string s; int64_t total; void countOneLetters(){ char last = s[0]; int64_t subseqSize = 0; for(char c: s){ if(c == last){ subseqSize++; } else { total += subseqSize * (subseqSize + 1) / 2; subseqSize = 1; last = c; } } total += subseqSize * (subseqSize + 1) / 2; } void countTwoLetters(){ struct Counter{ std::map<int, int64_t> diffs; int currDiff = 0; Counter(){ diffs[0] = 1; } void update(int d){ currDiff += d; diffs[currDiff]++; } void reset(){ for(auto [diff, points]: diffs){ total += points * (points - 1) / 2; } currDiff = 0; diffs.clear(); diffs[0] = 1; } } counters[3]; for(char c: s){ int i = c - 'a'; counters[i].update(1); counters[(i+1)%3].update(-1); counters[(i+2)%3].reset(); } counters[0].reset(); counters[1].reset(); counters[2].reset(); } void countThreeLetters(){ std::map<std::pair<int, int>, int64_t> diffs; std::pair<int, int64_t> currDiff = {0, 0}; diffs[currDiff] = 1; for(char c: s){ if(c == 'a'){ currDiff.first++; } else if(c == 'b'){ currDiff.second++; } else { currDiff.first--; currDiff.second--; } diffs[currDiff]++; } for(auto [diff, points]: diffs){ total += points * (points - 1) / 2; } } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cin >> s; countOneLetters(); countTwoLetters(); countThreeLetters(); std::cout << total; return 0; } |