#include <iostream> using std::cin; using std::cout; using std::getline; using word_t = std::string; namespace { inline char const letter_a = 'a'; inline size_t const num_of_letters = 3; bool check_balance(size_t counters[], size_t size = 3) { size_t balance_count = 0; for (size_t k = 0; k < size; ++k) { if (counters[k] > 0) { if (balance_count > 0 && balance_count != counters[k]) { return false; } else { if (balance_count == 0) balance_count = counters[k]; } } } return balance_count != 0; } size_t subwords(word_t const & word, size_t beg, size_t end) { size_t size = end - beg + 1; if (size == 1) { return 1; } else { if (size == 2) { return 3; } else { // Here are at least 3 letters size_t balanced_count = 0; size_t counters[num_of_letters]; // Boundary letters (word[beg] and word[end]) // are always balanced balanced_count += 2; // Resetting counters for (size_t k = 0; k < num_of_letters; ++k) counters[k] = 0; // Initialising a letter counter according to word[beg] counters[word[beg] - letter_a] = 1; // Counting in the range for (size_t k = beg + 1; k <= end; ++k) { counters[word[k] - letter_a] += 1; if (check_balance(counters)) ++balanced_count; } for (size_t k = beg; k < end - 1; ++k) { counters[word[k] - letter_a] -= 1; if (check_balance(counters)) ++balanced_count; } // Returning the result return balanced_count + subwords(word, beg + 1, end - 1); } } } } int main() { word_t word; size_t word_size; // Reading input getline(cin, word); word_size = word.size(); // Outputting result cout << subwords(word, 0, word_size - 1); 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 87 88 | #include <iostream> using std::cin; using std::cout; using std::getline; using word_t = std::string; namespace { inline char const letter_a = 'a'; inline size_t const num_of_letters = 3; bool check_balance(size_t counters[], size_t size = 3) { size_t balance_count = 0; for (size_t k = 0; k < size; ++k) { if (counters[k] > 0) { if (balance_count > 0 && balance_count != counters[k]) { return false; } else { if (balance_count == 0) balance_count = counters[k]; } } } return balance_count != 0; } size_t subwords(word_t const & word, size_t beg, size_t end) { size_t size = end - beg + 1; if (size == 1) { return 1; } else { if (size == 2) { return 3; } else { // Here are at least 3 letters size_t balanced_count = 0; size_t counters[num_of_letters]; // Boundary letters (word[beg] and word[end]) // are always balanced balanced_count += 2; // Resetting counters for (size_t k = 0; k < num_of_letters; ++k) counters[k] = 0; // Initialising a letter counter according to word[beg] counters[word[beg] - letter_a] = 1; // Counting in the range for (size_t k = beg + 1; k <= end; ++k) { counters[word[k] - letter_a] += 1; if (check_balance(counters)) ++balanced_count; } for (size_t k = beg; k < end - 1; ++k) { counters[word[k] - letter_a] -= 1; if (check_balance(counters)) ++balanced_count; } // Returning the result return balanced_count + subwords(word, beg + 1, end - 1); } } } } int main() { word_t word; size_t word_size; // Reading input getline(cin, word); word_size = word.size(); // Outputting result cout << subwords(word, 0, word_size - 1); return 0; } |