#pragma GCC optimize "Ofast" #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; #define f first #define s second #define sz(s) (int)s.size() #define all(s) s.begin(), s.end() #define pb push_back #define mp make_pair #define FOR(i, n) for (int i = 0; i < n; i++) #define ll long long #define ld long double #define vi vector<int> #define vvi vector<vi> #define ii pair<int, int> #define vii vector<ii> #define vvii vector<vii> #define vviii vector<vector<pair<int, ii>>> #define vll vector<ll> #define pll pair<ll, ll> #define vpll vector<pll> template <class T>ostream &operator<<(ostream &os, vector<T> &vec){for (T &el : vec){os << el << ' ';}return os;} template <class T>istream &operator>>(istream &is, vector<T> &vec) {for (T &el : vec){is >> el;}return is;} template <class T, class G> ostream &operator<<(ostream &os, pair<T, G> para) { os << para.f << ' ' << para.s; return os;} const ll BIG_INF = 1e18; const int INF = 1e9 + 7; // const int mod = 998244353; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define losuj(a, b) uniform_int_distribution<long long>(a, b)(rng) template <class T> void setmax(T & a, T b){a = max(a, b);} template <class T> void setmin(T & a, T b){a = min(a, b);} // #define K long double // #define KK pair<K, K> string bez(string s, char lit){ string ret; for(auto & u : s){ if(u != lit){ ret.push_back(u); } } return ret; } void solve() { string s; cin >> s; ll ans = 0; int np = 0; FOR(i, 26){ vi poz; FOR(j, sz(s)){ if(s[j] == ('a' + i)){ poz.pb(j); } } np += (sz(poz) & 1); FOR(j, (sz(poz) + 1) / 2){ ans += abs(poz[j] - (sz(s) - 1 - poz[sz(poz) - 1 - j])); } s = bez(s, 'a' + i); } cout << (np <= 1 ? ans : -1) << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; while(t--) { solve(); } }
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #pragma GCC optimize "Ofast" #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; #define f first #define s second #define sz(s) (int)s.size() #define all(s) s.begin(), s.end() #define pb push_back #define mp make_pair #define FOR(i, n) for (int i = 0; i < n; i++) #define ll long long #define ld long double #define vi vector<int> #define vvi vector<vi> #define ii pair<int, int> #define vii vector<ii> #define vvii vector<vii> #define vviii vector<vector<pair<int, ii>>> #define vll vector<ll> #define pll pair<ll, ll> #define vpll vector<pll> template <class T>ostream &operator<<(ostream &os, vector<T> &vec){for (T &el : vec){os << el << ' ';}return os;} template <class T>istream &operator>>(istream &is, vector<T> &vec) {for (T &el : vec){is >> el;}return is;} template <class T, class G> ostream &operator<<(ostream &os, pair<T, G> para) { os << para.f << ' ' << para.s; return os;} const ll BIG_INF = 1e18; const int INF = 1e9 + 7; // const int mod = 998244353; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define losuj(a, b) uniform_int_distribution<long long>(a, b)(rng) template <class T> void setmax(T & a, T b){a = max(a, b);} template <class T> void setmin(T & a, T b){a = min(a, b);} // #define K long double // #define KK pair<K, K> string bez(string s, char lit){ string ret; for(auto & u : s){ if(u != lit){ ret.push_back(u); } } return ret; } void solve() { string s; cin >> s; ll ans = 0; int np = 0; FOR(i, 26){ vi poz; FOR(j, sz(s)){ if(s[j] == ('a' + i)){ poz.pb(j); } } np += (sz(poz) & 1); FOR(j, (sz(poz) + 1) / 2){ ans += abs(poz[j] - (sz(s) - 1 - poz[sz(poz) - 1 - j])); } s = bez(s, 'a' + i); } cout << (np <= 1 ? ans : -1) << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; while(t--) { solve(); } } |