#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string a; int n, x=0, y=0, s=0; cin >> a; n=a.length(); for (int i = 0; i < n/2; i++){ if(a[i]=='a') x++; else y++; if(a[n-1-i]=='a') x++; else y++; if(a[i]!=a[n-1-i]) s++; } if(n%2==1&&a[n/2+1]=='a') x++; if(n%2==1&&a[n/2+1]=='b') y++; if(n%2==1&&((a[n/2+1]=='b'&&x%2==1)||(a[n/2+1]=='a'&&y%2==1))) s++; s/=2; if(y%2==1&&x%2==1) cout << -1; else 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 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string a; int n, x=0, y=0, s=0; cin >> a; n=a.length(); for (int i = 0; i < n/2; i++){ if(a[i]=='a') x++; else y++; if(a[n-1-i]=='a') x++; else y++; if(a[i]!=a[n-1-i]) s++; } if(n%2==1&&a[n/2+1]=='a') x++; if(n%2==1&&a[n/2+1]=='b') y++; if(n%2==1&&((a[n/2+1]=='b'&&x%2==1)||(a[n/2+1]=='a'&&y%2==1))) s++; s/=2; if(y%2==1&&x%2==1) cout << -1; else cout << s; } |