// Karol Kosinski 2022
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i)
#define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i)
#define ALL(c) (c).begin(),(c).end()
#define SIZE(c) int((c).size())
#define TIE(x...) int x;tie(x)
#define X first
#define Y second
#ifndef ENABLE_DEBUG
#define DEB(k,p,f,x...)
#else
#define DEB(k,p,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);if(p)f(x);}
#endif
#define DEBL DEB(1,1,void,0)
#define DEBF(f,x...) DEB(1,1,f,x)
#define DEBC(p,x...) DEB(0,p,printf,x)
#define DEBUG(x...) DEB(0,1,printf,x)
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
constexpr int NX = 200'005;
char A[NX], B[NX];
LL solve(int n, int k, char c)
{
LL res = 0;
copy( A, A + n, B );
reverse( B, B + n );
int i = 0, j = 0;
while ( k -- )
{
while ( A[i] != c ) ++ i;
while ( B[j] != c ) ++ j;
res += abs( i - j );
++ i, ++ j;
}
return res;
}
int main()
{
scanf("%s", A);
int n = strlen(A);
int an = count( A, A + n, 'a' ), bn = count( A, A + n, 'b' );
if ( an % 2 == 0 )
printf("%lld\n", solve( n, an / 2, 'a' ) );
else if ( bn % 2 == 0 )
printf("%lld\n", solve( n, bn / 2, 'b' ) );
else
printf("-1\n");
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 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 | // Karol Kosinski 2022 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define TIE(x...) int x;tie(x) #define X first #define Y second #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);if(p)f(x);} #endif #define DEBL DEB(1,1,void,0) #define DEBF(f,x...) DEB(1,1,f,x) #define DEBC(p,x...) DEB(0,p,printf,x) #define DEBUG(x...) DEB(0,1,printf,x) using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; constexpr int NX = 200'005; char A[NX], B[NX]; LL solve(int n, int k, char c) { LL res = 0; copy( A, A + n, B ); reverse( B, B + n ); int i = 0, j = 0; while ( k -- ) { while ( A[i] != c ) ++ i; while ( B[j] != c ) ++ j; res += abs( i - j ); ++ i, ++ j; } return res; } int main() { scanf("%s", A); int n = strlen(A); int an = count( A, A + n, 'a' ), bn = count( A, A + n, 'b' ); if ( an % 2 == 0 ) printf("%lld\n", solve( n, an / 2, 'a' ) ); else if ( bn % 2 == 0 ) printf("%lld\n", solve( n, bn / 2, 'b' ) ); else printf("-1\n"); return 0; } |
English