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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<bits/stdc++.h>

using namespace std;

int main(){
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
	
	map< pair<int, pair<int,int> >,int> mp;
	
	string s;
	
	cin>>s;
	int A,B,C;A=B=C=0;
	
	long long X=0;
	int Seria=0;
	map <pair<int,int>, int > podw;
	
	char xser,ff,sc;xser=ff=sc='p';
	int fr=0,ss=0;
	for(int i=0;i<s.size();i++){
		
		
		
		if(s[i]=='a')A++;
		else if(s[i]=='b')B++;
		else C++;
		
		if(A>0 and B>0 and C>0){
			A--;B--;C--;
		}
		if(s[i]!=ff and s[i]!=sc){
			podw.clear();
			for(int j=0;j<=Seria;j++){
				podw[{j,0}]++;
			}
			ff=xser;sc=s[i];
			fr=Seria;ss=0;
		}
		
		if(i==0){
			ff=xser=s[i];podw[{0,0}]++;mp[{0,{0,0}}]++;
		}
		
		
		
		if(i==0 or s[i]==s[i-1]){
			Seria++;
			//X+=Seria;
			xser=s[i];
			if(xser==ff){
				fr++;
			}
			else{
				ss++;				
			}
			if(fr>0 and ss>0){
				fr--;ss--;
			}
			X+=podw[{fr,ss}]++;
			
		}
		else{
			Seria=1;xser=s[i];
			if(sc=='p')sc=s[i];
			if(xser==ff){
				fr++;
			}
			else{
				ss++;				
			}
			if(fr>0 and ss>0){
				fr--;ss--;
			}
			//cout<<fr<<" "<<ss<<" "<<X<<" "<<podw[{fr,ss}]<<"\n";
			
			X+=podw[{fr,ss}]++;
			//podw[{fr,ss}]++;
		}
		//cout<<i<<" "<<X<<" "<<fr<<" "<<ss<<"\n";
		
		X+=mp[{A,{B,C}}]++;
		//cout<<"  "<<X<<"\n";
		X+=Seria;
		//cout<<"  "<<X<<" "<<podw[{1,0}]<<"\n";
	}
	
	cout<<X;
	
	
return 0;
}