// Jan Zachar 03/12/22
#include <iostream>
#include <string>
#define check(x) (str[x] == str[str.length()-1-x])
using namespace std;
string str;
int cntr;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
cin >> str;
for (int i = 0; i < str.length()-1; i++) {
if (check(i)) continue;
else if (str[i] != str[i+1]) {
swap(str[i], str[i+1]);
cntr++;
}
}
for (int i = str.length()-1; i > 0; i--) {
if (check(i)) continue;
else if (str[i] != str[i-1]) {
swap(str[i], str[i-1]);
cntr++;
}
}
for (int i = 0; i < str.length()-1; i++) {
if (!check(i)) {
cout << -1 << endl;
return 0;
}
}
cout << cntr << 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 | // Jan Zachar 03/12/22 #include <iostream> #include <string> #define check(x) (str[x] == str[str.length()-1-x]) using namespace std; string str; int cntr; int main() { ios_base::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); cin >> str; for (int i = 0; i < str.length()-1; i++) { if (check(i)) continue; else if (str[i] != str[i+1]) { swap(str[i], str[i+1]); cntr++; } } for (int i = str.length()-1; i > 0; i--) { if (check(i)) continue; else if (str[i] != str[i-1]) { swap(str[i], str[i-1]); cntr++; } } for (int i = 0; i < str.length()-1; i++) { if (!check(i)) { cout << -1 << endl; return 0; } } cout << cntr << endl; return 0; } |
English