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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;

vector<int> W;
map<int, int> M2;
int cnt2[3];
map<PII, int> M3;
int cnt3[3];


int main()
{
    ios_base::sync_with_stdio(0);
    string s;
    cin>>s;
    int n=s.size();
    for(int i=0;i<n;i++)W.push_back(s[i]-'a');
    long long res=0;
    
    int last=-1;
    int cnt=0;
    for(int x: W)
    {
        if(x==last)cnt++;
        else cnt=1;
        last=x;
        res+=cnt;
    }

    for(int d=0; d<=2; d++)
    {
        M2.clear();
        M2[0]=1;
        cnt2[0]=0, cnt2[1]=0, cnt2[2]=0;
        int i1=(d+1)%3, i2=(d+2)%3;
        for(int x: W)
        {
            if(x==d)
            {
                M2.clear();
                M2[0]=1;
                cnt2[0]=0, cnt2[1]=0, cnt2[2]=0;
                continue;
            }
            
            cnt2[x]++;
            res+=M2[cnt2[i1]-cnt2[i2]];
            M2[cnt2[i1]-cnt2[i2]]++;
        }
    }

    M3[{0,0}]=1;
    for(int x: W)
    {
        cnt3[x]++;
        res+=M3[{cnt3[0]-cnt3[1],cnt3[1]-cnt3[2]}];
        M3[{cnt3[0]-cnt3[1],cnt3[1]-cnt3[2]}]++;
    }
    cout<<res<<endl;
}