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
// Krzysztof Baranski (2021.12.03)
#include <cstdio>
#include <unordered_map>

using namespace std;

typedef long long LL;

char input[300001];
LL char_map[200];

LL count_subsequences_with_zero_sum(LL a_value, LL b_value, LL c_value) {
	unordered_map<LL, LL> T;
	T.clear();
	T[0] = 1;

	char_map['a'] = a_value;
	char_map['b'] = b_value;
	char_map['c'] = c_value;

	LL sum = 0;
	LL count = 0;
	int i = 0;
	unordered_map<LL, LL>::const_iterator s;
	while(input[i] != '\0') {
		sum += char_map[input[i++]];
		if((s = T.find(sum)) != T.end()) {
			count += s->second;
			T[sum] = s->second + 1;
		} else {
			T[sum] = 1;
		}
	}
	return count;
}

int main() {
	scanf("%s", input);

	LL result = 0;
	result += count_subsequences_with_zero_sum(0, 1, 1); // count A
	result += count_subsequences_with_zero_sum(1, 0, 1); // count B
	result += count_subsequences_with_zero_sum(1, 1, 0); // count C
	result += count_subsequences_with_zero_sum(1, -1, 1000000); // count A+B
	result += count_subsequences_with_zero_sum(1000000, 1, -1); // count B+C
	result += count_subsequences_with_zero_sum(1, 1000000, -1); // count A+C
	result += count_subsequences_with_zero_sum(1000000, 1, -1000001); // count A+B+C

	printf("%lld\n", result);
}