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
#include<bits/stdc++.h>
#define para pair<int, int> 
using namespace std;

constexpr int MAXN = 3e5+7;

string s;
int n;

map <para, int> mapa3;
map <para, int> mapa2a;
map <para, int> mapa1a;
map <para, int> mapa2b;
map <para, int> mapa1b;
map <para, int> mapa2c;
map <para, int> mapa1c;

long long res = 0;

int a_b, b_c, c_a, a, b, c;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> s;
    n = s.size();
    s = "#" + s;
    
    mapa3[{0,0}] = 1;
    mapa2a[{0,0}] = 1;
    mapa2b[{0,0}] = 1;
    mapa2c[{0,0}] = 1;
    mapa1a[{0,0}] = 1;
    mapa1b[{0,0}] = 1;
    mapa1c[{0,0}] = 1;
    
    for (int i = 1; i <= n; i++)
    {
        if (s[i] == 'a'){
            a++;
            a_b++;
            c_a--;
        }
        else if (s[i] == 'b'){
            b++;
            b_c++;
            a_b--;
        }
        else {
            c++;
            c_a++;
            b_c--;
        }

        para curr;

        curr = {a_b, b_c}; res += mapa3[curr]; mapa3[curr]++; /// ABC
        curr = {a, b_c}; res += mapa2a[curr]; mapa2a[curr]++; /// BC
        curr = {b, c_a}; res += mapa2b[curr]; mapa2b[curr]++; /// CA
        curr = {c, a_b}; res += mapa2c[curr]; mapa2c[curr]++; /// AB
        curr = {a, b}; res += mapa1c[curr]; mapa1c[curr]++;   /// C
        curr = {b, c}; res += mapa1a[curr]; mapa1a[curr]++;   /// A
        curr = {c, a}; res += mapa1b[curr]; mapa1b[curr]++;   /// B
        
    }

    cout << res << "\n";
    return 0;
}
// 7 map.... Moj rekord by far