#include <iostream> #include <set> #include <map> #include <algorithm> #include <cassert> using namespace std; void rewind_last_before(set<unsigned int> &pos, unsigned k) { while (pos.rbegin() != pos.rend() && *pos.rbegin() > k) { pos.erase(prev(pos.end())); } } void rewind_first_after(set<unsigned int> &pos, unsigned p) { while (pos.begin() != pos.end() && *pos.begin() < p) { pos.erase(pos.begin()); } } int main() { ios_base::sync_with_stdio(false); string word; cin >> word; map<char, set<unsigned >> pos; pos['a'] = set<unsigned>(); pos['b'] = set<unsigned>(); auto i = 0; for (const auto &c: word) { pos[c].insert(i); i++; } unsigned p = 0; unsigned k = word.length() - 1; long a_count = count(word.begin(), word.end(), 'a'); long b_count = count(word.begin(), word.end(), 'b'); if (a_count % 2 == 1 && b_count % 2 == 1) { cout << -1 << endl; } else { unsigned long long cnt = 0; while (p < k) { if (word[p] != word[k]) { rewind_first_after(pos[word[k]], p); rewind_last_before(pos[word[p]], k); auto first_pos = pos[word[k]].begin(); auto last_pos = prev(pos[word[p]].end()); if (*first_pos - p < k - *last_pos) { pos[word[p]].erase(p); pos[word[p]].insert(*first_pos); pos[word[k]].erase(*first_pos); word[*first_pos] = word[p]; word[p] = word[k]; cnt += *first_pos - p; } else { pos[word[k]].erase(k); pos[word[k]].insert(*last_pos); pos[word[p]].erase(*last_pos); word[*last_pos] = word[k]; word[k] = word[p]; cnt += k - *last_pos; } } p++; k--; } cout << cnt << endl; } 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 | #include <iostream> #include <set> #include <map> #include <algorithm> #include <cassert> using namespace std; void rewind_last_before(set<unsigned int> &pos, unsigned k) { while (pos.rbegin() != pos.rend() && *pos.rbegin() > k) { pos.erase(prev(pos.end())); } } void rewind_first_after(set<unsigned int> &pos, unsigned p) { while (pos.begin() != pos.end() && *pos.begin() < p) { pos.erase(pos.begin()); } } int main() { ios_base::sync_with_stdio(false); string word; cin >> word; map<char, set<unsigned >> pos; pos['a'] = set<unsigned>(); pos['b'] = set<unsigned>(); auto i = 0; for (const auto &c: word) { pos[c].insert(i); i++; } unsigned p = 0; unsigned k = word.length() - 1; long a_count = count(word.begin(), word.end(), 'a'); long b_count = count(word.begin(), word.end(), 'b'); if (a_count % 2 == 1 && b_count % 2 == 1) { cout << -1 << endl; } else { unsigned long long cnt = 0; while (p < k) { if (word[p] != word[k]) { rewind_first_after(pos[word[k]], p); rewind_last_before(pos[word[p]], k); auto first_pos = pos[word[k]].begin(); auto last_pos = prev(pos[word[p]].end()); if (*first_pos - p < k - *last_pos) { pos[word[p]].erase(p); pos[word[p]].insert(*first_pos); pos[word[k]].erase(*first_pos); word[*first_pos] = word[p]; word[p] = word[k]; cnt += *first_pos - p; } else { pos[word[k]].erase(k); pos[word[k]].insert(*last_pos); pos[word[p]].erase(*last_pos); word[*last_pos] = word[k]; word[k] = word[p]; cnt += k - *last_pos; } } p++; k--; } cout << cnt << endl; } return 0; } |