#include <iostream> #include <vector> using namespace std; string s; int S, n; vector <int> b; int main() { cin >> s; n = s.length(); for (int i = 0; i < n; i++) { if (s[i] == 'b') b.push_back(i); } if (n % 2 == 0) { if (b.size() % 2) { cout << -1; return 0; } } else if (b.size() % 2) S += abs(n / 2 - b.at(b.size() / 2)); for (int i = 0; i < b.size() / 2; i++) { S += abs(b.at(i) - (n - 1 - b.at(b.size() - 1 - i))); } cout << S; }
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 | #include <iostream> #include <vector> using namespace std; string s; int S, n; vector <int> b; int main() { cin >> s; n = s.length(); for (int i = 0; i < n; i++) { if (s[i] == 'b') b.push_back(i); } if (n % 2 == 0) { if (b.size() % 2) { cout << -1; return 0; } } else if (b.size() % 2) S += abs(n / 2 - b.at(b.size() / 2)); for (int i = 0; i < b.size() / 2; i++) { S += abs(b.at(i) - (n - 1 - b.at(b.size() - 1 - i))); } cout << S; } |