#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int i; string s; cin>>s; int n = s.size(); int ile1 = 0; int ile2 = 0; for (i = 0; i < n; i++){ if (s[i] == 'a') ile1++; else ile2++; } //cout<<ile1<<" "<<ile2<<"\n"; if (n%2){ if (ile1%2 && ile2%2){ cout<<"-1\n"; return 0; } }else if (ile1%2 || ile2%2){ cout<<"-1\n"; return 0; } int wy = 0; for (i = 0; i < n/2; i++){ if (s[i] == s[n-i-1]) continue; wy++; if (s[i+1] == s[n-i-1]){ swap(s[i],s[i+1]); }else{ swap(s[n-i-1],s[n-i-2]); } } cout<<wy<<"\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 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int i; string s; cin>>s; int n = s.size(); int ile1 = 0; int ile2 = 0; for (i = 0; i < n; i++){ if (s[i] == 'a') ile1++; else ile2++; } //cout<<ile1<<" "<<ile2<<"\n"; if (n%2){ if (ile1%2 && ile2%2){ cout<<"-1\n"; return 0; } }else if (ile1%2 || ile2%2){ cout<<"-1\n"; return 0; } int wy = 0; for (i = 0; i < n/2; i++){ if (s[i] == s[n-i-1]) continue; wy++; if (s[i+1] == s[n-i-1]){ swap(s[i],s[i+1]); }else{ swap(s[n-i-1],s[n-i-2]); } } cout<<wy<<"\n"; return 0; } |