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
#include<bits/stdc++.h>
using namespace std;
char arr[300000];
map <long long,int> mapa_abc;
map <long long,int> mapa_ab;
map <long long,int> mapa_bc;
map <long long,int> mapa_ac;
int main(){
	int n,i,a,b,c,mabc;
	long long tmp,result,k;
	scanf("%s",arr);
	n=0;
	while(arr[n]!=0)
		n++;
	k=1000000;
	result=0;
	tmp=0;
	a=b=c=0;
	mapa_abc[0]=1;
	mapa_ab[0]=1;
	mapa_bc[0]=1;
	mapa_ac[0]=1;
	for(i=0;i<n;i++){
		if(arr[i]=='a')
			a++;
		if(arr[i]=='b')
			b++;
		if(arr[i]=='c')
			c++;
		mabc=min({a,b,c});
		tmp=(a-mabc)*k*k+(b-mabc)*k+(c-mabc);
		//printf("i: %d\na: %d b: %d c: %d\nabc tmp: %lld  +%d\n",i,a,b,c,tmp,mapa_abc[tmp]);
		result+=mapa_abc[tmp];
		mapa_abc[tmp]++;
		
		mabc=min({a,b});
		tmp=(a-mabc)*k*k+(b-mabc)*k+(c);
		//printf("ab tmp: %lld  +%d\n",tmp,mapa_ab[tmp]);
		result+=mapa_ab[tmp];
		mapa_ab[tmp]++;
		
		mabc=min({b,c});
		tmp=(a)*k*k+(b-mabc)*k+(c-mabc);
		//printf("bc tmp: %lld  +%d\n",tmp,mapa_bc[tmp]);
		result+=mapa_bc[tmp];
		mapa_bc[tmp]++;
		
		mabc=min({a,c});
		tmp=(a-mabc)*k*k+(b)*k+(c-mabc);
		//printf("ac tmp: %lld  +%d\n\n",tmp,mapa_ac[tmp]);
		result+=mapa_ac[tmp];
		mapa_ac[tmp]++;
	}
	i=0;
	while(i<n){
		k=i+1;
		while(k<(long long)n && arr[i]==arr[k])
			k++;
		//printf("%d-%lld : %c\n",i,k,arr[i]);
		result+=(k-i)*(k-i+1)/2;
		i=k;
	}	
	printf("%lld\n",result);
	return 0;
}