#include <iostream>
#include <queue>
#include <stack>
using namespace std;
int abs(int &n);
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int res = 0;
string s;
cin>>s;
int as = 0, ss = s.size();
queue<int> q1;
stack<int> q2;
for(int i=0; i<ss; i++)
{
if(s[i]=='a')
q1.push(i);
}
as = q1.size();
if(as%2==1 && ss%2==0)
{
res = -1;
}
else
{
for(int i=0; i<as/2; i++)
{
q2.push(q1.front());
q1.pop();
}
if(as%2==1)
{
res += abs(ss/2 - q1.front());
q1.pop();
}
while(!q1.empty())
{
res += abs(ss - 1 - q1.front() - q2.top());
q1.pop();
q2.pop();
}
}
cout<<res;
return 0;
}
int abs(int &n)
{
return (n>=0?n:-n);
}
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 | #include <iostream> #include <queue> #include <stack> using namespace std; int abs(int &n); int main() { ios_base::sync_with_stdio(0); cin.tie(0); int res = 0; string s; cin>>s; int as = 0, ss = s.size(); queue<int> q1; stack<int> q2; for(int i=0; i<ss; i++) { if(s[i]=='a') q1.push(i); } as = q1.size(); if(as%2==1 && ss%2==0) { res = -1; } else { for(int i=0; i<as/2; i++) { q2.push(q1.front()); q1.pop(); } if(as%2==1) { res += abs(ss/2 - q1.front()); q1.pop(); } while(!q1.empty()) { res += abs(ss - 1 - q1.front() - q2.top()); q1.pop(); q2.pop(); } } cout<<res; return 0; } int abs(int &n) { return (n>=0?n:-n); } |
English