#include<iostream> #include<vector> #include<string> using namespace std; long long check(long long a, long long b, long long c) { if (a == b && b == c) return 1; if (a == b && c == 0) return 1; if (a == c && b == 0) return 1; if (b == c && a == 0) return 1; if (a == 0 && b == 0) return 1; if (b == 0 && c == 0) return 1; if (a == 0 && c == 0) return 1; return 0; } long long sub(vector<long long>& v, int a, int b) { long long res = v[b]; if (a > 0) { res -= v[a - 1]; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string str; cin >> str; int n = str.length(); vector<long long> va(n); vector<long long> vb(n); vector<long long> vc(n); for (int i = 0; i < n; i++) { if (i > 0) { va[i] = va[i - 1]; vb[i] = vb[i - 1]; vc[i] = vc[i - 1]; } if (str[i] == 'a') { va[i]++; } if (str[i] == 'b') { vb[i]++; } if (str[i] == 'c') { vc[i]++; } } long long res = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { long long a = sub(va, i, j); long long b = sub(vb, i, j); long long c = sub(vc, i, j); res += check(a, b, c); } } 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 | #include<iostream> #include<vector> #include<string> using namespace std; long long check(long long a, long long b, long long c) { if (a == b && b == c) return 1; if (a == b && c == 0) return 1; if (a == c && b == 0) return 1; if (b == c && a == 0) return 1; if (a == 0 && b == 0) return 1; if (b == 0 && c == 0) return 1; if (a == 0 && c == 0) return 1; return 0; } long long sub(vector<long long>& v, int a, int b) { long long res = v[b]; if (a > 0) { res -= v[a - 1]; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string str; cin >> str; int n = str.length(); vector<long long> va(n); vector<long long> vb(n); vector<long long> vc(n); for (int i = 0; i < n; i++) { if (i > 0) { va[i] = va[i - 1]; vb[i] = vb[i - 1]; vc[i] = vc[i - 1]; } if (str[i] == 'a') { va[i]++; } if (str[i] == 'b') { vb[i]++; } if (str[i] == 'c') { vc[i]++; } } long long res = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { long long a = sub(va, i, j); long long b = sub(vb, i, j); long long c = sub(vc, i, j); res += check(a, b, c); } } cout << res; return 0; } |