#include <unordered_map> #include <array> #include <cstdio> using namespace std; template<> struct std::hash<pair<int,int>> { size_t operator()(pair<int,int> const& d) const { return hash<long long>{}( (1LL<<20)*(d.second+30000) + d.first+30000 ); } }; int main() { unordered_map<pair<int,int>, int> count3; unordered_map<int, int> count2[3]; char last = '\0'; int count1 = 0; int counts[3] = {0}; int counts_pairs[3][3] = {{0}}; int c, inp = 0, diff; long long result = 0; while((c = getchar()) > 0){ if((c & 0b11111100) == 0x60) { if (c != last) { result += 1LL*count1*(count1+1) / 2; count1 = 1; last = c; } else { ++count1; } inp = c - 'a'; for (int i = 0; i < 3; ++i) counts_pairs[inp][i] = 0; ++counts[inp]; for (int i = 0; i < 3; ++i) ++counts_pairs[i][inp]; count2[inp].clear(); for (int i = 0; i < 3; ++i) { if (i != inp) { diff = counts_pairs[i][(i+2)%3] - counts_pairs[i][(i+1)%3]; count2[i][diff] += 1; //printf("%d %d %d %d\n", inp, i, diff, count2[i][diff]); result += count2[i][diff] - (diff == 0 ? 0 : 1); } } auto diffs = make_pair(counts[2]-counts[1], counts[1]-counts[0]); count3[diffs] += 1; result += count3[diffs] - (diffs.first == 0 && diffs.second == 0 ? 0 : 1); } } result += 1LL*count1*(count1+1) / 2; printf("%lld\n", result); }
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 | #include <unordered_map> #include <array> #include <cstdio> using namespace std; template<> struct std::hash<pair<int,int>> { size_t operator()(pair<int,int> const& d) const { return hash<long long>{}( (1LL<<20)*(d.second+30000) + d.first+30000 ); } }; int main() { unordered_map<pair<int,int>, int> count3; unordered_map<int, int> count2[3]; char last = '\0'; int count1 = 0; int counts[3] = {0}; int counts_pairs[3][3] = {{0}}; int c, inp = 0, diff; long long result = 0; while((c = getchar()) > 0){ if((c & 0b11111100) == 0x60) { if (c != last) { result += 1LL*count1*(count1+1) / 2; count1 = 1; last = c; } else { ++count1; } inp = c - 'a'; for (int i = 0; i < 3; ++i) counts_pairs[inp][i] = 0; ++counts[inp]; for (int i = 0; i < 3; ++i) ++counts_pairs[i][inp]; count2[inp].clear(); for (int i = 0; i < 3; ++i) { if (i != inp) { diff = counts_pairs[i][(i+2)%3] - counts_pairs[i][(i+1)%3]; count2[i][diff] += 1; //printf("%d %d %d %d\n", inp, i, diff, count2[i][diff]); result += count2[i][diff] - (diff == 0 ? 0 : 1); } } auto diffs = make_pair(counts[2]-counts[1], counts[1]-counts[0]); count3[diffs] += 1; result += count3[diffs] - (diffs.first == 0 && diffs.second == 0 ? 0 : 1); } } result += 1LL*count1*(count1+1) / 2; printf("%lld\n", result); } |