#include <array> #include <iostream> #include <string> int main() { std::ios_base::sync_with_stdio(0); std::string w; std::getline(std::cin, w); size_t result = 0; std::array<int, 3> counts = {0, 0, 0}; for (size_t start_idx = 0; start_idx < w.size(); ++start_idx) { counts = {0, 0, 0}; counts[w[start_idx] - 'a'] = 1; ++result; // Single letter is always ok. if (start_idx + 1 < w.size()) { // Two-letter is always ok. ++counts[w[start_idx + 1] - 'a']; ++result; } for (size_t end_idx = start_idx + 2; end_idx < w.size(); ++end_idx) { const char c = w[end_idx]; ++counts[c - 'a']; bool is_ok = true; int expected_count = counts[w[start_idx] - 'a']; for (int x : counts) { if (x != 0 && x != expected_count) { is_ok = false; break; } } if (is_ok) { result += 1; } // std::cerr << "start=" << start_idx << ",end=" << end_idx << ",ok=" << is_ok << std::endl; // if (start_idx == 1 && end_idx == 2) { // // std::cerr << "start_counts=" << start_counts[0] << ',' << start_counts[1] << ',' << start_counts[2] << std::endl; // std::cerr << "counts=" << counts[0] << ',' << counts[1] << ',' << counts[2] << std::endl; // } } } std::cout << result << std::endl; }
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 | #include <array> #include <iostream> #include <string> int main() { std::ios_base::sync_with_stdio(0); std::string w; std::getline(std::cin, w); size_t result = 0; std::array<int, 3> counts = {0, 0, 0}; for (size_t start_idx = 0; start_idx < w.size(); ++start_idx) { counts = {0, 0, 0}; counts[w[start_idx] - 'a'] = 1; ++result; // Single letter is always ok. if (start_idx + 1 < w.size()) { // Two-letter is always ok. ++counts[w[start_idx + 1] - 'a']; ++result; } for (size_t end_idx = start_idx + 2; end_idx < w.size(); ++end_idx) { const char c = w[end_idx]; ++counts[c - 'a']; bool is_ok = true; int expected_count = counts[w[start_idx] - 'a']; for (int x : counts) { if (x != 0 && x != expected_count) { is_ok = false; break; } } if (is_ok) { result += 1; } // std::cerr << "start=" << start_idx << ",end=" << end_idx << ",ok=" << is_ok << std::endl; // if (start_idx == 1 && end_idx == 2) { // // std::cerr << "start_counts=" << start_counts[0] << ',' << start_counts[1] << ',' << start_counts[2] << std::endl; // std::cerr << "counts=" << counts[0] << ',' << counts[1] << ',' << counts[2] << std::endl; // } } } std::cout << result << std::endl; } |