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

#define FOR(i,a,b) for(int (i)=(int)(a); (i)!=(int)(b); ++(i))

char S[300005];
int count[3][300005], sum[3];

int main() {
    scanf("%s", S);
    int len = strlen(S);

    FOR(i,0,3) {
        FOR(j,0,len) count[i][j] = 0;
        sum[i] = 0;
    }

    FOR(i,0,len) {
        if (i>0) FOR(k,0,3) count[k][i] = count[k][i-1];
        ++sum[S[i]-'a'];
        ++count[S[i]-'a'][i];
    }

    int result = 0;
    FOR(i,0,len) FOR(j,i,len) {
        int cnt[3];
        FOR(k,0,3) {
            cnt[k] = count[k][j];
            if (i>0) cnt[k] -= count[k][i-1];
        }
        if (cnt[0] && cnt[1] && cnt[0]!=cnt[1]) continue;
        if (cnt[0] && cnt[2] && cnt[0]!=cnt[2]) continue;
        if (cnt[1] && cnt[2] && cnt[1]!=cnt[2]) continue;
        ++result;
    }

    printf("%d\n", result);
}