#include <bits/stdc++.h> using namespace std; using ll = long long; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int n = s.size(); ll ans = 0; int x; vector<char> p = {'a','b','c'}; vector<char> p1 = {'a','b','a'}; vector<char> p2 = {'b','c','c'}; int cur; for (int j = 0; j < 3; j++){ cur = 0; for (int i = 0; i < n; i++){ if (s[i] == p[j]){ cur++; ans += cur; } else cur = 0; } } map<int, int> abb; for (int j = 0; j < 3; j++){ x = 0; abb.clear(); abb[0] = 1; for (int i = 0; i < n; i++){ if (s[i] == p1[j]) x++; else if (s[i] == p2[j]) x--; else { x = 0; abb.clear(); abb[0] = 1; continue; } ans += abb[x]; abb[x]++; } } int ab = 0; int bc = 0; map<pair<int, int>, int> ile; ile[make_pair(0, 0)] = 1; for (int i = 0; i < n; i++){ if (s[i] == 'a') ab++; else if (s[i] == 'c') bc--; else{ ab--; bc++; } ans += ile[make_pair(ab, bc)]; ile[make_pair(ab, bc)]++; } cout << ans << endl; 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 | #include <bits/stdc++.h> using namespace std; using ll = long long; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int n = s.size(); ll ans = 0; int x; vector<char> p = {'a','b','c'}; vector<char> p1 = {'a','b','a'}; vector<char> p2 = {'b','c','c'}; int cur; for (int j = 0; j < 3; j++){ cur = 0; for (int i = 0; i < n; i++){ if (s[i] == p[j]){ cur++; ans += cur; } else cur = 0; } } map<int, int> abb; for (int j = 0; j < 3; j++){ x = 0; abb.clear(); abb[0] = 1; for (int i = 0; i < n; i++){ if (s[i] == p1[j]) x++; else if (s[i] == p2[j]) x--; else { x = 0; abb.clear(); abb[0] = 1; continue; } ans += abb[x]; abb[x]++; } } int ab = 0; int bc = 0; map<pair<int, int>, int> ile; ile[make_pair(0, 0)] = 1; for (int i = 0; i < n; i++){ if (s[i] == 'a') ab++; else if (s[i] == 'c') bc--; else{ ab--; bc++; } ans += ile[make_pair(ab, bc)]; ile[make_pair(ab, bc)]++; } cout << ans << endl; return 0; } |