#include <algorithm> #include <cstdint> #include <ios> #include <iostream> #include <iterator> #include <string> #include <vector> using std::swap; template <typename T> void print_value(const T &v); template <typename T> void debug(const T &v, const std::string &s) { std::cout << s << ": "; print_value(v); std::cout << std::endl; } template <> void print_value(const std::vector<int32_t> &v) { for (const auto x : v) { std::cout << x << " "; } } template <> void print_value(const int &v) { std::cout << v; } void reverse(std::vector<int32_t> &pos, int32_t n) { for (int i = 0; i < pos.size() / 2; ++i) { int i_from_end = pos.size() - i - 1; swap(pos[i], pos[i_from_end]); } for (auto &x : pos) { x = n - x - 1; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::string ab_str; std::getline(std::cin, ab_str); std::vector<int32_t> apos; const int n = ab_str.length(); apos.reserve(n); int i = 0; for (const auto &c : ab_str) { if (c == 'a') { apos.push_back(i); } ++i; } if (apos.size() % 2 == 1 && ab_str.size() % 2 == 0) { std::cout << "-1\n"; return 0; } std::vector<int32_t> first_half; std::vector<int32_t> second_half; for (int i = 0; i < apos.size(); ++i) { if (i < apos.size() / 2) { first_half.push_back(apos[i]); } else { second_half.push_back(apos[i]); } } reverse(second_half, n); if (*first_half.rbegin() > *second_half.rbegin()) { swap(first_half, second_half); } int result = 0; for (int i = 0; i < std::min(first_half.size(), second_half.size()); ++i) { result += std::abs(first_half[i] - second_half[i]); } if (first_half.size() != second_half.size()) { if (first_half.size() < second_half.size()) { swap(first_half, second_half); } result += std::abs(*first_half.rbegin() - n/2); } std::cout << result << "\n"; 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 | #include <algorithm> #include <cstdint> #include <ios> #include <iostream> #include <iterator> #include <string> #include <vector> using std::swap; template <typename T> void print_value(const T &v); template <typename T> void debug(const T &v, const std::string &s) { std::cout << s << ": "; print_value(v); std::cout << std::endl; } template <> void print_value(const std::vector<int32_t> &v) { for (const auto x : v) { std::cout << x << " "; } } template <> void print_value(const int &v) { std::cout << v; } void reverse(std::vector<int32_t> &pos, int32_t n) { for (int i = 0; i < pos.size() / 2; ++i) { int i_from_end = pos.size() - i - 1; swap(pos[i], pos[i_from_end]); } for (auto &x : pos) { x = n - x - 1; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::string ab_str; std::getline(std::cin, ab_str); std::vector<int32_t> apos; const int n = ab_str.length(); apos.reserve(n); int i = 0; for (const auto &c : ab_str) { if (c == 'a') { apos.push_back(i); } ++i; } if (apos.size() % 2 == 1 && ab_str.size() % 2 == 0) { std::cout << "-1\n"; return 0; } std::vector<int32_t> first_half; std::vector<int32_t> second_half; for (int i = 0; i < apos.size(); ++i) { if (i < apos.size() / 2) { first_half.push_back(apos[i]); } else { second_half.push_back(apos[i]); } } reverse(second_half, n); if (*first_half.rbegin() > *second_half.rbegin()) { swap(first_half, second_half); } int result = 0; for (int i = 0; i < std::min(first_half.size(), second_half.size()); ++i) { result += std::abs(first_half[i] - second_half[i]); } if (first_half.size() != second_half.size()) { if (first_half.size() < second_half.size()) { swap(first_half, second_half); } result += std::abs(*first_half.rbegin() - n/2); } std::cout << result << "\n"; return 0; } |