#include <iostream> int main() { std::string word; std::cin >> word; int balanced = 0; for (int start = 0; start < word.length(); start++) { int abc[3] = {0, 0, 0}; for (int end = start; end < word.length(); end++) { switch (word[end]) { case 'a': abc[0]++; break; case 'b': abc[1]++; break; case 'c': abc[2]++; } int expected = 0; bool isBalanced = true; for (int i = 0; i < 3; i++) { if (abc[i] > 0) { if (expected == 0) expected = abc[i]; if (abc[i] != expected) isBalanced = false; } } if (isBalanced) balanced++; } } std::cout << balanced; 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 | #include <iostream> int main() { std::string word; std::cin >> word; int balanced = 0; for (int start = 0; start < word.length(); start++) { int abc[3] = {0, 0, 0}; for (int end = start; end < word.length(); end++) { switch (word[end]) { case 'a': abc[0]++; break; case 'b': abc[1]++; break; case 'c': abc[2]++; } int expected = 0; bool isBalanced = true; for (int i = 0; i < 3; i++) { if (abc[i] > 0) { if (expected == 0) expected = abc[i]; if (abc[i] != expected) isBalanced = false; } } if (isBalanced) balanced++; } } std::cout << balanced; return 0; } |