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
#include <iostream>
using namespace std;

const int maxN = 3e5+5;
int a[maxN], b[maxN], c[maxN];

void solve()
{
    string w;
    int ans = 0, n, al, bl, cl, ending, x;
    cin >> w;

    n = w.size();

    for(int i = 1; i <= n; i++) {
        if(w[i-1] == 'a')
            a[i]++;
        if(w[i-1] == 'b')
            b[i]++;
        if(w[i-1] == 'c')
            c[i]++;

        a[i] += a[i-1];
        b[i] += b[i-1];
        c[i] += c[i-1];
    }

    for(int length = 1; length <= n; length++) {
        for(int start = 1; start <= n - length + 1; start++) {
            ending = start + length - 1;
            al = a[ending] - a[start - 1];
            bl = b[ending] - b[start - 1];
            cl = c[ending] - c[start - 1];

            x = max(max(al,bl),cl);

            if(al == 0)
                al = x;
            if(bl == 0)
                bl = x;
            if(cl == 0)
                cl = x;

            if(al == bl && bl == cl)
                ans++;
        }
    }

    cout << ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    solve();

    return 0;
}