#include <iostream>
#include <string>
#include <unordered_map>
int main()
{
std::unordered_map<int, std::unordered_map<int, int>> count_a_c_over_b;
std::unordered_map<int, std::unordered_map<int, int>> count_b_c_over_a;
std::unordered_map<int, std::unordered_map<int, int>> count_c_b_over_a;
std::unordered_map<int, std::unordered_map<int, int>> b_c_over_a;
std::unordered_map<int, std::unordered_map<int, int>> count_a_b;
std::unordered_map<int, std::unordered_map<int, int>> count_b_c;
std::unordered_map<int, std::unordered_map<int, int>> count_a_c;
std::string s;
std::cin >> s;
int a = 0, b = 0, c = 0;
long long total = 0;
int c_over_a = 0;
int b_over_a = 0;
int c_over_b = 0;
++count_a_c_over_b[a][c_over_b];
++count_b_c_over_a[b][c_over_a];
++count_c_b_over_a[c][b_over_a];
++b_c_over_a[b_over_a][c_over_a];
++count_a_b[a][b];
++count_a_c[a][c];
++count_b_c[b][c];
for (char ch : s) {
switch (ch) {
case 'a':
++a;
c_over_a = c - a;
b_over_a = b - a;
total += count_b_c_over_a[b][c_over_a];
total += count_c_b_over_a[c][b_over_a];
total += count_b_c[b][c];
break;
case 'b':
++b;
c_over_b = c - b;
b_over_a = b - a;
total += count_a_c_over_b[a][c_over_b];
total += count_c_b_over_a[c][b_over_a];
total += count_a_c[a][c];
break;
case 'c':
++c;
c_over_a = c - a;
c_over_b = c - b;
total += count_a_c_over_b[a][c_over_b];
total += count_b_c_over_a[b][c_over_a];
total += count_a_b[a][b];
break;
default:
break;
}
total += b_c_over_a[b_over_a][c_over_a];
++count_a_c_over_b[a][c_over_b];
++count_b_c_over_a[b][c_over_a];
++count_c_b_over_a[c][b_over_a];
++b_c_over_a[b_over_a][c_over_a];
++count_a_b[a][b];
++count_a_c[a][c];
++count_b_c[b][c];
}
std::cout << total << std::endl;
}
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 | #include <iostream> #include <string> #include <unordered_map> int main() { std::unordered_map<int, std::unordered_map<int, int>> count_a_c_over_b; std::unordered_map<int, std::unordered_map<int, int>> count_b_c_over_a; std::unordered_map<int, std::unordered_map<int, int>> count_c_b_over_a; std::unordered_map<int, std::unordered_map<int, int>> b_c_over_a; std::unordered_map<int, std::unordered_map<int, int>> count_a_b; std::unordered_map<int, std::unordered_map<int, int>> count_b_c; std::unordered_map<int, std::unordered_map<int, int>> count_a_c; std::string s; std::cin >> s; int a = 0, b = 0, c = 0; long long total = 0; int c_over_a = 0; int b_over_a = 0; int c_over_b = 0; ++count_a_c_over_b[a][c_over_b]; ++count_b_c_over_a[b][c_over_a]; ++count_c_b_over_a[c][b_over_a]; ++b_c_over_a[b_over_a][c_over_a]; ++count_a_b[a][b]; ++count_a_c[a][c]; ++count_b_c[b][c]; for (char ch : s) { switch (ch) { case 'a': ++a; c_over_a = c - a; b_over_a = b - a; total += count_b_c_over_a[b][c_over_a]; total += count_c_b_over_a[c][b_over_a]; total += count_b_c[b][c]; break; case 'b': ++b; c_over_b = c - b; b_over_a = b - a; total += count_a_c_over_b[a][c_over_b]; total += count_c_b_over_a[c][b_over_a]; total += count_a_c[a][c]; break; case 'c': ++c; c_over_a = c - a; c_over_b = c - b; total += count_a_c_over_b[a][c_over_b]; total += count_b_c_over_a[b][c_over_a]; total += count_a_b[a][b]; break; default: break; } total += b_c_over_a[b_over_a][c_over_a]; ++count_a_c_over_b[a][c_over_b]; ++count_b_c_over_a[b][c_over_a]; ++count_c_b_over_a[c][b_over_a]; ++b_c_over_a[b_over_a][c_over_a]; ++count_a_b[a][b]; ++count_a_c[a][c]; ++count_b_c[b][c]; } std::cout << total << std::endl; } |
English