#include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define pb push_back int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; rep(i, 2) { vector<int>V; rep(j, s.size()) if(s[j]=='a'+i) V.pb(j); if(V.size()%2==1) continue; ll ans=0; rep(j, V.size()/2) { ll x=s.size()-V[V.size()-j-1]-1; ans+=abs((long long)V[j]-x); } cout << ans << '\n'; return 0; } cout << -1 << '\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define pb push_back int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; rep(i, 2) { vector<int>V; rep(j, s.size()) if(s[j]=='a'+i) V.pb(j); if(V.size()%2==1) continue; ll ans=0; rep(j, V.size()/2) { ll x=s.size()-V[V.size()-j-1]-1; ans+=abs((long long)V[j]-x); } cout << ans << '\n'; return 0; } cout << -1 << '\n'; } |