#include <iostream> #include <set> using namespace std; bool checkIfBalanced(string str) { int count[3] = { 0 }; for (int i = 0; i < str.length(); i++) { count[str[i]-97]++; } set<int> checker; checker.insert(count, count + 3); checker.erase(0); return checker.size() == 1; } int main() { int balancedCount = 0; string zdanie; cin >> zdanie; for (int i = 1; i < zdanie.length(); i++) { for (int j = 0; j <= zdanie.length() - i; j++) { balancedCount += checkIfBalanced(zdanie.substr(j, i)); } } cout << balancedCount; 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 | #include <iostream> #include <set> using namespace std; bool checkIfBalanced(string str) { int count[3] = { 0 }; for (int i = 0; i < str.length(); i++) { count[str[i]-97]++; } set<int> checker; checker.insert(count, count + 3); checker.erase(0); return checker.size() == 1; } int main() { int balancedCount = 0; string zdanie; cin >> zdanie; for (int i = 1; i < zdanie.length(); i++) { for (int j = 0; j <= zdanie.length() - i; j++) { balancedCount += checkIfBalanced(zdanie.substr(j, i)); } } cout << balancedCount; return 0; } |