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
#include <iostream>

using namespace std;

int pref[300000][3];

int main() {
    ios_base::sync_with_stdio(0);
    string s;
    cin >> s;
    pref[0][s[0] - 'a'] = 1;
    for(int i = 1; i < s.size(); i++) {
        pref[i][0] = pref[i-1][0]; 
        pref[i][1] = pref[i-1][1]; 
        pref[i][2] = pref[i-1][2]; 
        pref[i][s[i] - 'a']++;
        // cout << pref[i][0] << " " << pref[i][1] << " " << pref[i][2] << "\n";
    }
    long long cnt = 0;

    for(int dl = 1; dl < s.size(); dl++) {
        for(int start = 0; start < s.size()-dl+1; start++) {
            int a, b, c;
            if(start==0) {
                a = pref[dl-1][0];
                b = pref[dl-1][1];
                c = pref[dl-1][2];
            } else {
                a = pref[start+dl-1][0] - pref[start-1][0];
                b = pref[start+dl-1][1] - pref[start-1][1];
                c = pref[start+dl-1][2] - pref[start-1][2];
            }
            // cout << dl << " " << start << "\n" << "\n";
            if((a == b || a==0 || b == 0) && (c == b || c==0 || b == 0) && (a == c || a==0 || c == 0)) {
                cnt++;
            }
        }
    }
    cout << cnt << "\n";
    return 0;
}