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
#include <bits/stdc++.h>
using namespace std;
constexpr size_t limit = 3e5+5, llimit = log2(limit)+1, smaska=0xFFFFFFFF>>(32-llimit-1);
struct MyHash
{
    size_t operator()(tuple<long long,long long,long long> const& s) const
    {
        return (get<0>(s)&smaska)<<((llimit+1)<<1) | (get<1>(s)&smaska)<<(llimit+1) | (get<2>(s)&smaska);
    }
};
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	string s;
	getline(cin, s);
	unordered_map<tuple<long long, long long, long long>, long long, MyHash> m;
	long long cnt[3] = {0,0,0};
	unsigned long long suma=0, suma7=0, suma6=0, suma5=0, suma4=0, suma3=0, suma2=0, suma1=0;
	++m[{0,0,0}];
	++m[{-1,0,0}];
	++m[{0,-1,0}];
	++m[{0,0,-1}];
	char lc = '\0';
	long long ll=0;
	for(auto &&e : s) {
		cnt[e-'a']++;
		long long minimum = *min_element(cnt, cnt+3);
		long long minimum12 = min(cnt[1], cnt[2]);
		long long minimum02 = min(cnt[0], cnt[2]);
		long long minimum01 = min(cnt[0], cnt[1]);
		suma7 += m[{cnt[0]-minimum,   cnt[1]-minimum,   cnt[2]-minimum}]++;
		suma6 += m[{-1-cnt[0],        cnt[1]-minimum12, cnt[2]-minimum12}]++;
		suma5 += m[{cnt[0]-minimum02, -1-cnt[1],        cnt[2]-minimum02}]++;
		suma4 += m[{cnt[0]-minimum01, cnt[1]-minimum01, -1-cnt[2]}]++;
		if(lc==e) suma += ++ll;
		else lc=e, suma += ll=1;
	}
	suma += suma7+suma6+suma5+suma4;
	cout << suma << '\n';
}