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
#include <iostream>
#include <algorithm>

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);

	std::string s;
	std::cin >> s;
	if (s.size() % 2 == 0) {
		if (std::count(s.begin(), s.end(), 'a') % 2 == 1) {
			std::cout << -1 << '\n';
			return 0;
		}
	}

	int sekund = 0;
	int l = 0;
	int p = s.size() - 1;
	for (;l < p; ++l, --p) {
		int i = l;
		while (s[p] != s[i]) {
			++i;
		}
		if (i == p) {
			sekund += (p-l)/2;
			//std::cerr << "|" << '\n';
			break;
		} else {
			sekund += i-l;
			//std::cerr << l << "↔" << i << '\n';
			char tmp = s[i];
			s[i] = s[l];
			s[l] = tmp;
		}
	}

	std::cout << sekund << '\n';
}