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
#include <cstdio>

int main()
{
char str[300001];
long long int cnt = 0,N,i,l[3],j,prv;

	scanf("%s",str);
//printf("%s\n",str);

	N = 0;
	while (str[N]) N++;
	cnt += N;
	cnt += N - 1;
	
	for (i = 3; i <= N; i++)
	{
		l['a'-'a'] = l['b'-'a'] = l['c'-'a'] = 0;
		for (j = 0; j < i-1; j++) l[str[j]-'a']++;
//printf("Debug init: i=%lld a=%lld b=%lld c=%lld\n",i,l[0],l[1],l[2]);
		while (str[j])
		{
//printf("Debug in: i=%lld a=%lld b=%lld c=%lld\n",i,l[0],l[1],l[2]);
			l[str[j]-'a']++;
			if (j-i >= 0) l[str[j-i]-'a']--;
//printf("Debug out: i=%lld a=%lld b=%lld c=%lld\n",i,l[0],l[1],l[2]);
			prv = cnt;
			if (l[0] > 0 && l[1] == 0 && l[2] == 0) cnt++;
			if (l[0] == 0 && l[1] > 0 && l[2] == 0) cnt++;
			if (l[0] == 0 && l[1] == 0 && l[2] > 0) cnt++;
			if (l[0] > 0 && l[1] > 0 && l[2] == 0 && l[0] == l[1]) cnt++;
			if (l[0] > 0 && l[1] == 0 && l[2] > 0 && l[0] == l[2]) cnt++;
			if (l[0] == 0 && l[1] > 0 && l[2] > 0 && l[1] == l[2]) cnt++;
			if (l[0] > 0 && l[1] > 0 && l[2] > 0 && l[0] == l[1] && l[0] == l[2]) cnt++;
//if (prv < cnt) {printf("hit: %lld %lld\n",i,j);	for (prv = j-i+1; prv <= j; prv++) printf("%c",str[prv]);printf("\n");}
			j++;
		}
	}
	
	printf("%lld\n",cnt);
	
	return 0;
}