#include <bits/stdc++.h> using namespace std; vector <int> v; string s; long long r; int main() { cin>>s; for(int i=0;i<s.length();i++) { if(s[i]=='a') v.push_back(i); } if(!(s.length()&1)&&(v.size()&1)) { cout<<"-1"<<endl; return 0; } for(int i=0;i<(v.size()+1)/2;i++) { r+=(long long)abs((int)s.length()-v[i]-v[v.size()-i-1]-1); } cout<<r<<endl; 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 | #include <bits/stdc++.h> using namespace std; vector <int> v; string s; long long r; int main() { cin>>s; for(int i=0;i<s.length();i++) { if(s[i]=='a') v.push_back(i); } if(!(s.length()&1)&&(v.size()&1)) { cout<<"-1"<<endl; return 0; } for(int i=0;i<(v.size()+1)/2;i++) { r+=(long long)abs((int)s.length()-v[i]-v[v.size()-i-1]-1); } cout<<r<<endl; return 0; } |