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
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 300005;

char t[N];
string s;
map<int, int> m1;
map<ii, int> m2;
int b, c, i, j, k, n, tab[N], act;
LL res;

int main() {
scanf("%s", t);
s = t;
n = s.size();
for (i=0;i<n;i++) tab[i] = int(s[i] - 'a');
tab[n] = 4;

res = 0;
act = 1;
for (i=1;i<=n;i++) {
	if (tab[i] != tab[i - 1]) {
		res += (act * 1LL * (act + 1) / 2);
		act = 0;
	}
	act++;
}

for (i=0;i<3;i++) for (j=i+1;j<3;j++) {
	m1.clear();
	act = 0;
	m1[0]++;
	for (k=0;k<n;k++) {
		if (tab[k] != i && tab[k] != j) {
			m1.clear();
			act = 0;
			m1[0]++;
		} else {
			if (tab[k] == i) act++; else act--;
			res += m1[act];
			m1[act]++;
		}
	}
}

m2.clear();
m2[ii(0, 0)]++;
b = c = 0;
for (i=0;i<n;i++) {
	if (tab[i] == 0) {
		b--;
		c--;
	} else if (tab[i] == 1) b++; else c++;
	res += m2[ii(b, c)];
	m2[ii(b, c)]++;
}
printf("%lld\n", res);
return 0;
}