#include <iostream> #include <vector> #include <map> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string input; cin >> input; vector<int> count(3); long long result = 0; char lastLetter = 'a'; int current = 0; map<pair<int, int>, int> statesA, statesB, statesC, states; statesA[{ 0, 0 }] = 1; statesB[{ 0, 0 }] = 1; statesC[{ 0, 0 }] = 1; states[{ 0, 0 }] = 1; for(int index = 0; index < input.size(); index++) { count[input[index] - 'a']++; if(lastLetter == input[index]) { current++; } else { lastLetter = input[index]; current = 1; } result += current; result += statesA[{ count[0], count[2] - count[1] }]; result += statesB[{ count[1], count[2] - count[0] }]; result += statesC[{ count[2], count[1] - count[0] }]; result += states[{ count[1] - count[0], count[2] - count[1] }]; statesA[{ count[0], count[2] - count[1] }]++; statesB[{ count[1], count[2] - count[0] }]++; statesC[{ count[2], count[1] - count[0] }]++; states[{ count[1] - count[0], count[2] - count[1] }]++; } cout << result << '\n'; }
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 | #include <iostream> #include <vector> #include <map> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string input; cin >> input; vector<int> count(3); long long result = 0; char lastLetter = 'a'; int current = 0; map<pair<int, int>, int> statesA, statesB, statesC, states; statesA[{ 0, 0 }] = 1; statesB[{ 0, 0 }] = 1; statesC[{ 0, 0 }] = 1; states[{ 0, 0 }] = 1; for(int index = 0; index < input.size(); index++) { count[input[index] - 'a']++; if(lastLetter == input[index]) { current++; } else { lastLetter = input[index]; current = 1; } result += current; result += statesA[{ count[0], count[2] - count[1] }]; result += statesB[{ count[1], count[2] - count[0] }]; result += statesC[{ count[2], count[1] - count[0] }]; result += states[{ count[1] - count[0], count[2] - count[1] }]; statesA[{ count[0], count[2] - count[1] }]++; statesB[{ count[1], count[2] - count[0] }]++; statesC[{ count[2], count[1] - count[0] }]++; states[{ count[1] - count[0], count[2] - count[1] }]++; } cout << result << '\n'; } |