#include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); string x; cin>>x; vector <long long> v; for(long long i=0; i<x.size(); i++){ if(x[i]=='a'){ v.push_back(i); } } if(x.size()%2==0&&v.size()%2==1){ cout<<"-1"; return 0; } long long wynik=0; for(long long i=0; i<v.size()/2; i++){ wynik+=max(v[i]-(long long)x.size()+1+v[v.size()-1-i], (long long)x.size()-1-v[v.size()-1-i]-v[i]); } if(v.size()%2==1){ wynik+=max((long long)x.size()/2-v[v.size()/2], v[v.size()/2]-(long long)x.size()/2); } cout<<wynik; }
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 | #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); string x; cin>>x; vector <long long> v; for(long long i=0; i<x.size(); i++){ if(x[i]=='a'){ v.push_back(i); } } if(x.size()%2==0&&v.size()%2==1){ cout<<"-1"; return 0; } long long wynik=0; for(long long i=0; i<v.size()/2; i++){ wynik+=max(v[i]-(long long)x.size()+1+v[v.size()-1-i], (long long)x.size()-1-v[v.size()-1-i]-v[i]); } if(v.size()%2==1){ wynik+=max((long long)x.size()/2-v[v.size()/2], v[v.size()/2]-(long long)x.size()/2); } cout<<wynik; } |