#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool CzyZbalansowane(string podslowo, int dlugosc_podslowa) {
bool a, b, c;
a = b = c = false;
int cnt_a, cnt_b, cnt_c;
cnt_a = cnt_b = cnt_c = 0;
for (int i = 0; i < dlugosc_podslowa; ++i)
{
if (podslowo[i] == 'a')
{
a = true;
++cnt_a;
}
else if(podslowo[i] == 'b')
{
b = true;
++cnt_b;
}
else
{
c = true;
++cnt_c;
}
}
if (a && b && c)
{
if (cnt_a == cnt_b && cnt_b == cnt_c)
{
return true;
}
return false;
}
if (a && b)
{
if (cnt_a == cnt_b)
{
return true;
}
return false;
}
if (b && c)
{
if (cnt_b == cnt_c)
{
return true;
}
return false;
}
if (c && a)
{
if (cnt_c == cnt_a)
{
return true;
}
return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
string slowo;
cin >> slowo;
int d = slowo.length();
ll liczba_zbalansowanych_podslow = d;
for (int i = 2; i <= d; ++i)
{
for (int j = 0; j <= d-i; ++j)
{
if (CzyZbalansowane(slowo.substr(j, i), i))
{
++liczba_zbalansowanych_podslow;
}
}
}
cout << liczba_zbalansowanych_podslow << '\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; bool CzyZbalansowane(string podslowo, int dlugosc_podslowa) { bool a, b, c; a = b = c = false; int cnt_a, cnt_b, cnt_c; cnt_a = cnt_b = cnt_c = 0; for (int i = 0; i < dlugosc_podslowa; ++i) { if (podslowo[i] == 'a') { a = true; ++cnt_a; } else if(podslowo[i] == 'b') { b = true; ++cnt_b; } else { c = true; ++cnt_c; } } if (a && b && c) { if (cnt_a == cnt_b && cnt_b == cnt_c) { return true; } return false; } if (a && b) { if (cnt_a == cnt_b) { return true; } return false; } if (b && c) { if (cnt_b == cnt_c) { return true; } return false; } if (c && a) { if (cnt_c == cnt_a) { return true; } return false; } return true; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL); string slowo; cin >> slowo; int d = slowo.length(); ll liczba_zbalansowanych_podslow = d; for (int i = 2; i <= d; ++i) { for (int j = 0; j <= d-i; ++j) { if (CzyZbalansowane(slowo.substr(j, i), i)) { ++liczba_zbalansowanych_podslow; } } } cout << liczba_zbalansowanych_podslow << '\n'; return 0; } |
English