#include <iostream> bool checkWord(std::string word) { unsigned int counterA = 0, counterB = 0, counterC = 0; for(char letter: word) { switch(letter) { case 'a': counterA++; break; case 'b': counterB++; break; case 'c': counterC++; break; } } if(counterA == 0) { if(counterB == 0) { return true; } if(counterC == 0) { return true; } return counterB == counterC; } if(counterB == 0) { if(counterC == 0) { return true; } return counterC == counterA; } if(counterC == 0) { return counterB == counterA; } return counterA == counterB == counterC; } int main() { std::string word; std::string resultWord; unsigned int result = 0; std::cin >> word; for(int idxStart = 0; idxStart < word.length(); idxStart++) { resultWord = ""; for(int idxCurrent = idxStart; idxCurrent < word.length(); idxCurrent++) { resultWord += word[idxCurrent]; if(checkWord(resultWord)) { result++; } } } std::cout << result; }
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 | #include <iostream> bool checkWord(std::string word) { unsigned int counterA = 0, counterB = 0, counterC = 0; for(char letter: word) { switch(letter) { case 'a': counterA++; break; case 'b': counterB++; break; case 'c': counterC++; break; } } if(counterA == 0) { if(counterB == 0) { return true; } if(counterC == 0) { return true; } return counterB == counterC; } if(counterB == 0) { if(counterC == 0) { return true; } return counterC == counterA; } if(counterC == 0) { return counterB == counterA; } return counterA == counterB == counterC; } int main() { std::string word; std::string resultWord; unsigned int result = 0; std::cin >> word; for(int idxStart = 0; idxStart < word.length(); idxStart++) { resultWord = ""; for(int idxCurrent = idxStart; idxCurrent < word.length(); idxCurrent++) { resultWord += word[idxCurrent]; if(checkWord(resultWord)) { result++; } } } std::cout << result; } |