#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> PII; const int MAX_N = 3e5+5; int n; string s; map<PII, int> cnt; int cnt_balance[2 * MAX_N]; bool check(vector<int> vec) { sort(vec.begin(), vec.end()); return vec[0] == vec.back(); } void brute_force() { vector<vector<int>> pref(n + 5, vector<int>(3, 0)); for (int i = 1; i <= n; i++) { pref[i][0] = pref[i-1][0]; pref[i][1] = pref[i-1][1]; pref[i][2] = pref[i-1][2]; pref[i][s[i] - 'a']++; } int ans = 0; for (int l = 1; l <= n; l++) { for (int r = l; r <= n; r++) { int A = pref[r][0] - pref[l-1][0]; int B = pref[r][1] - pref[l-1][1]; int C = pref[r][2] - pref[l-1][2]; vector<int> vec; if (A > 0) vec.push_back(A); if (B > 0) vec.push_back(B); if (C > 0) vec.push_back(C); ans += check(vec); } } cout << ans << '\n'; } ll get(ll x) { return (x * (x + 1)) / 2; } ll one_letter() { ll res = 0LL; int i = 1; while (i <= n) { int j = i; while (j <= n && s[i] == s[j]) j++; res += get((ll)(j - i)); i = j; } //cout << "One: " << res << '\n'; return res; } ll count_equal(int L, int R, char a, char b) { ll res = 0LL; int len = R - L; for (int i = 0; i <= 2*len+3; i++) { cnt_balance[i] = 0; } cnt_balance[len]++; int cur = 0; for (int i = L; i < R; i++) { if (s[i] == a) cur++; else cur--; res += cnt_balance[cur + len]; cnt_balance[cur + len]++; } //cout << "Count equal "; //cout << L << ", " << R << ": " << res << '\n'; return res; } ll two_letters(char a, char b) { int L = 1; int R = 1; ll res = 0LL; for (int i = 1; i <= n; i++) { if (s[i] != a && s[i] != b) { if (L != R) res += count_equal(L, R, a, b); L = R = i + 1; } else { R++; } } if (L != R) res += count_equal(L, R, a, b); //cout << a << ", " << b << ": " << res << '\n'; return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s; n = s.size(); s = '#' + s; if (n <= 3005) { brute_force(); return 0; } ll ans = one_letter(); ans += two_letters('a', 'b'); ans += two_letters('b', 'c'); ans += two_letters('a', 'c'); int A = 0, B = 0, C = 0; cnt[{0, 0}]++; for (int i = 1; i <= n; i++) { if (s[i] == 'a') A++; else if (s[i] == 'b') B++; else C++; int diff_b = B - A; int diff_c = C - A; PII tmp = make_pair(diff_b, diff_c); //cout << i << ": " << cnt[tmp] << '\n'; ans += cnt[tmp]; cnt[tmp]++; } cout << ans << '\n'; 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> PII; const int MAX_N = 3e5+5; int n; string s; map<PII, int> cnt; int cnt_balance[2 * MAX_N]; bool check(vector<int> vec) { sort(vec.begin(), vec.end()); return vec[0] == vec.back(); } void brute_force() { vector<vector<int>> pref(n + 5, vector<int>(3, 0)); for (int i = 1; i <= n; i++) { pref[i][0] = pref[i-1][0]; pref[i][1] = pref[i-1][1]; pref[i][2] = pref[i-1][2]; pref[i][s[i] - 'a']++; } int ans = 0; for (int l = 1; l <= n; l++) { for (int r = l; r <= n; r++) { int A = pref[r][0] - pref[l-1][0]; int B = pref[r][1] - pref[l-1][1]; int C = pref[r][2] - pref[l-1][2]; vector<int> vec; if (A > 0) vec.push_back(A); if (B > 0) vec.push_back(B); if (C > 0) vec.push_back(C); ans += check(vec); } } cout << ans << '\n'; } ll get(ll x) { return (x * (x + 1)) / 2; } ll one_letter() { ll res = 0LL; int i = 1; while (i <= n) { int j = i; while (j <= n && s[i] == s[j]) j++; res += get((ll)(j - i)); i = j; } //cout << "One: " << res << '\n'; return res; } ll count_equal(int L, int R, char a, char b) { ll res = 0LL; int len = R - L; for (int i = 0; i <= 2*len+3; i++) { cnt_balance[i] = 0; } cnt_balance[len]++; int cur = 0; for (int i = L; i < R; i++) { if (s[i] == a) cur++; else cur--; res += cnt_balance[cur + len]; cnt_balance[cur + len]++; } //cout << "Count equal "; //cout << L << ", " << R << ": " << res << '\n'; return res; } ll two_letters(char a, char b) { int L = 1; int R = 1; ll res = 0LL; for (int i = 1; i <= n; i++) { if (s[i] != a && s[i] != b) { if (L != R) res += count_equal(L, R, a, b); L = R = i + 1; } else { R++; } } if (L != R) res += count_equal(L, R, a, b); //cout << a << ", " << b << ": " << res << '\n'; return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s; n = s.size(); s = '#' + s; if (n <= 3005) { brute_force(); return 0; } ll ans = one_letter(); ans += two_letters('a', 'b'); ans += two_letters('b', 'c'); ans += two_letters('a', 'c'); int A = 0, B = 0, C = 0; cnt[{0, 0}]++; for (int i = 1; i <= n; i++) { if (s[i] == 'a') A++; else if (s[i] == 'b') B++; else C++; int diff_b = B - A; int diff_c = C - A; PII tmp = make_pair(diff_b, diff_c); //cout << i << ": " << cnt[tmp] << '\n'; ans += cnt[tmp]; cnt[tmp]++; } cout << ans << '\n'; return 0; } |