#include <iostream> #include <string> #include <vector> int main() { std::string str; getline(std::cin, str); size_t len = str.length(); std::vector<size_t> num_of_as(len + 1, 0); std::vector<size_t> num_of_bs(len + 1, 0); std::vector<size_t> num_of_cs(len + 1, 0); for (size_t i = 0; i < len; ++i) { num_of_as[i + 1] = num_of_as[i]; num_of_bs[i + 1] = num_of_bs[i]; num_of_cs[i + 1] = num_of_cs[i]; switch(str[i]) { case 'a': ++num_of_as[i + 1]; break; case 'b': ++num_of_bs[i + 1]; break; case 'c': ++num_of_cs[i + 1]; break; } } size_t result = 0; for (size_t i = 0; i < len; ++i) { for (size_t j = i; j < len; ++j) { const size_t nas = num_of_as[j + 1] - num_of_as[i]; const size_t nbs = num_of_bs[j + 1] - num_of_bs[i]; const size_t ncs = num_of_cs[j + 1] - num_of_cs[i]; if ((nas == 0 && nbs == 0) || (nas == 0 && ncs == 0) || (nbs == 0 && ncs == 0) || (nas == 0 && nbs == ncs) || (nbs == 0 && nas == ncs) || (ncs == 0 && nas == nbs) || ((nas == nbs) && (nbs == ncs))) { ++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 | #include <iostream> #include <string> #include <vector> int main() { std::string str; getline(std::cin, str); size_t len = str.length(); std::vector<size_t> num_of_as(len + 1, 0); std::vector<size_t> num_of_bs(len + 1, 0); std::vector<size_t> num_of_cs(len + 1, 0); for (size_t i = 0; i < len; ++i) { num_of_as[i + 1] = num_of_as[i]; num_of_bs[i + 1] = num_of_bs[i]; num_of_cs[i + 1] = num_of_cs[i]; switch(str[i]) { case 'a': ++num_of_as[i + 1]; break; case 'b': ++num_of_bs[i + 1]; break; case 'c': ++num_of_cs[i + 1]; break; } } size_t result = 0; for (size_t i = 0; i < len; ++i) { for (size_t j = i; j < len; ++j) { const size_t nas = num_of_as[j + 1] - num_of_as[i]; const size_t nbs = num_of_bs[j + 1] - num_of_bs[i]; const size_t ncs = num_of_cs[j + 1] - num_of_cs[i]; if ((nas == 0 && nbs == 0) || (nas == 0 && ncs == 0) || (nbs == 0 && ncs == 0) || (nas == 0 && nbs == ncs) || (nbs == 0 && nas == ncs) || (ncs == 0 && nas == nbs) || ((nas == nbs) && (nbs == ncs))) { ++result; } } } std::cout << result; } |