#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
string s;
vector<int> t;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long ans=0;
cin>>s;
for(size_t i=0;i<s.size();i++)
{
if(s[i]=='b')
t.push_back(i);
}
if(t.size()%2==1 && s.size()%2==0)
{
cout<<"-1\n";
return 0;
}
for(int i=0,j=(int)t.size()-1;i<=j;i++,j--)
{
if(i<j)
ans+=abs((int)s.size()-t[j]-1-t[i]);
else
ans+=abs(t[i]-(int)s.size()/2);
}
cout<<ans<<"\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 | #include<bits/stdc++.h> #define fi first #define se second using namespace std; string s; vector<int> t; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long ans=0; cin>>s; for(size_t i=0;i<s.size();i++) { if(s[i]=='b') t.push_back(i); } if(t.size()%2==1 && s.size()%2==0) { cout<<"-1\n"; return 0; } for(int i=0,j=(int)t.size()-1;i<=j;i++,j--) { if(i<j) ans+=abs((int)s.size()-t[j]-1-t[i]); else ans+=abs(t[i]-(int)s.size()/2); } cout<<ans<<"\n"; return 0; } |
English