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>
#include <cstring>
#include <vector>

using namespace std;

int main() {
    string str;
    cin >> str;
    int len = str.length();
    vector<int> ca(len+1);
    vector<int> cb(len+1);
    vector<int> cc(len+1);
    int res {len * 2 - 1};
    for (int i=1; i<=len; i++) {
        ca[i] = ca[i-1];
        cb[i] = cb[i-1];
        cc[i] = cc[i-1];
        if (str[i-1] == 'a') {
            ca[i]++;
        } else if (str[i-1] == 'b') {
            cb[i]++;
        } else {
            cc[i]++;
        };
        for (int j=0; j<i-2; j++) {
            int da = ca[i] - ca[j];
            int db = cb[i] - cb[j];
            int dc = cc[i] - cc[j];
            if (da == db && da == dc || da == db && dc == 0 ||
                da == dc && db == 0  || db == dc && da == 0 || 
                da == 0  && db == 0  || da == 0  && dc == 0 || 
                db == 0  && dc == 0) {
                res++;
            }
        }
    }

    cout << res << endl;
    return 0;
}