//Mateusz Kruk
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
string t; //tekst
cin>>t;
bool e=true;//e czy da sie
if(t.size()%2==0){
int x=0,y=0;
for(int i=0;i<t.size();i++){
if(t[i]=='a'){
x++;
}
else{
y++;
}
}
if(x%2==0&&y%2==0){
e=true;
}
else{
cout<<"-1"<<endl;
e=false;
}
}
if(e==true){
long long ans=0;
int j=t.size()-1;
for(int i=0;i<=j;i++){
if(t[i]!=t[j]){
int a=1,b=1;//a do i, b do j;
while(t[i+a]==t[i]){
a++;
}
while(t[j-b]==t[j]){
b++;
}
if(a>b){
ans+=b;
for(int k=0;k<b;k++){
swap(t[j-b+k],t[j-b+1+k]);
}
}
else{
ans+=a;
for(int k=0;k<a;k++){
swap(t[i+a-k],t[i+a-1-k]);
}
}
}
j--;
}
cout<<ans<<endl;
}
}
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 | //Mateusz Kruk #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); string t; //tekst cin>>t; bool e=true;//e czy da sie if(t.size()%2==0){ int x=0,y=0; for(int i=0;i<t.size();i++){ if(t[i]=='a'){ x++; } else{ y++; } } if(x%2==0&&y%2==0){ e=true; } else{ cout<<"-1"<<endl; e=false; } } if(e==true){ long long ans=0; int j=t.size()-1; for(int i=0;i<=j;i++){ if(t[i]!=t[j]){ int a=1,b=1;//a do i, b do j; while(t[i+a]==t[i]){ a++; } while(t[j-b]==t[j]){ b++; } if(a>b){ ans+=b; for(int k=0;k<b;k++){ swap(t[j-b+k],t[j-b+1+k]); } } else{ ans+=a; for(int k=0;k<a;k++){ swap(t[i+a-k],t[i+a-1-k]); } } } j--; } cout<<ans<<endl; } } |
English