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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
string s;
ll solvefor3()
{
    ll res = 0;
    map<pair<ll, ll>, ll> mp;
    mp[{0, 0}] = 1; // basic case
    ll as = 0, bs = 0, cs = 0;
    for (auto &i : s)
    {
        if (i == 'a')
            ++as;
        else if (i == 'b')
            ++bs;
        else
            ++cs;
        pair<ll, ll> tmp = {as - bs, as - cs};
        if (as && bs && cs)
            res += mp[tmp];
        ++mp[tmp];
    }
    return res;
}
ll solvefor1()
{
    ll res = 0;
    char cur = s[0];
    ll len = 0;
    for (char &c : s)
        if (c == cur)
            ++len;
        else
        {
            res += len * (len + 1) / 2; // always even
            cur = c;
            len = 1;
        }
    res += len * (len + 1) / 2; // always even
    return res;
}

ll solveforbc()
{
    ll res = 0;
    map<ll, ll> mp;
    mp[0] = 1; // basic case
    ll as = 0, bs = 0, cs = 0, dif;
    for (auto &i : s)
    {
        if (i == 'c')
            ++cs;
        else if (i == 'b')
            ++bs;
        else
        {
            mp.clear();
            mp[0] = 1;
            cs = 0;
            bs = 0;
            continue;
        }
        dif = cs - bs;
        if (cs && bs)
            res += mp[dif];
        ++mp[dif];
    }
    return res;
}
ll solveforac()
{
    ll res = 0;
    map<ll, ll> mp;
    mp[0] = 1; // basic case
    ll as = 0, bs = 0, cs = 0, dif;
    for (auto &i : s)
    {
        if (i == 'a')
            ++as;
        else if (i == 'c')
            ++cs;
        else
        {
            mp.clear();
            mp[0] = 1;
            as = 0;
            cs = 0;
            continue;
        }
        dif = as - cs;
        if (as && cs)
            res += mp[dif];
        ++mp[dif];
    }
    return res;
}
ll solveforab()
{
    ll res = 0;
    map<ll, ll> mp;
    mp[0] = 1; // basic case
    ll as = 0, bs = 0, cs = 0, dif;
    for (auto &i : s)
    {
        if (i == 'a')
            ++as;
        else if (i == 'b')
            ++bs;
        else
        {
            mp.clear();
            mp[0] = 1;
            as = 0;
            bs = 0;
            continue;
        }
        dif = as - bs;
        if (as && bs)
            res += mp[dif];
        ++mp[dif];
    }
    return res;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    cout << solvefor3() + solvefor1() + solveforab() + solveforac() + solveforbc() << '\n';
}