#include "bits/stdc++.h" using namespace std; typedef pair<int, int> Para; const int P = 1696969; namespace std { template<> struct hash<Para> { size_t operator() (const Para ¶) const { auto [b, c] = para; return ((b * P) + c) * P; } }; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; long long res = 0; char last = '0'; int sameLetterStreak = 0; unordered_map<int, int> ab, bc, ac; unordered_map<Para, int> abc; abc[{ 0, 0 }] = 1; ab[0] = 1; bc[0] = 1; ac[0] = 1; array<int, 3> letterCount = { 0, 0, 0 }; for(const auto &element : s) { if(last == element) sameLetterStreak++; else { sameLetterStreak = 1; last = element; } res += sameLetterStreak; letterCount[element - 'a']++; switch(element) { case 'a': bc = unordered_map<int, int>(); bc[letterCount[1] - letterCount[2]]++; res += ab[letterCount[0] - letterCount[1]]++; res += ac[letterCount[0] - letterCount[2]]++; break; case 'b': ac = unordered_map<int, int>(); ac[letterCount[0] - letterCount[2]]++; res += ab[letterCount[0] - letterCount[1]]++; res += bc[letterCount[1] - letterCount[2]]++; break; case 'c': ab = unordered_map<int, int>(); ab[letterCount[0] - letterCount[1]]++; res += ac[letterCount[0] - letterCount[2]]++; res += bc[letterCount[1] - letterCount[2]]++; break; } res += abc[{ letterCount[0] - letterCount[1], letterCount[0] - letterCount[2] }]++; } cout << res; return 0; }
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; typedef pair<int, int> Para; const int P = 1696969; namespace std { template<> struct hash<Para> { size_t operator() (const Para ¶) const { auto [b, c] = para; return ((b * P) + c) * P; } }; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; long long res = 0; char last = '0'; int sameLetterStreak = 0; unordered_map<int, int> ab, bc, ac; unordered_map<Para, int> abc; abc[{ 0, 0 }] = 1; ab[0] = 1; bc[0] = 1; ac[0] = 1; array<int, 3> letterCount = { 0, 0, 0 }; for(const auto &element : s) { if(last == element) sameLetterStreak++; else { sameLetterStreak = 1; last = element; } res += sameLetterStreak; letterCount[element - 'a']++; switch(element) { case 'a': bc = unordered_map<int, int>(); bc[letterCount[1] - letterCount[2]]++; res += ab[letterCount[0] - letterCount[1]]++; res += ac[letterCount[0] - letterCount[2]]++; break; case 'b': ac = unordered_map<int, int>(); ac[letterCount[0] - letterCount[2]]++; res += ab[letterCount[0] - letterCount[1]]++; res += bc[letterCount[1] - letterCount[2]]++; break; case 'c': ab = unordered_map<int, int>(); ab[letterCount[0] - letterCount[1]]++; res += ac[letterCount[0] - letterCount[2]]++; res += bc[letterCount[1] - letterCount[2]]++; break; } res += abc[{ letterCount[0] - letterCount[1], letterCount[0] - letterCount[2] }]++; } cout << res; return 0; } |