#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
int findNextR(char* pal, char letter, int start, int len)
{
while (start < len && pal[start] != letter)
{
start++;
}
return start;
}
int findNextL(char* pal, char letter, int start)
{
while (start >= 0 && pal[start] != letter)
{
start--;
}
return start;
}
int main()
{
char* pal = new char[200001];
scanf("%s", pal);
int l = strlen(pal);
int a = 0, b = 0;
for (int i = 0; i < l; i++)
{
if (pal[i] == 'a')
{
a++;
}
else
{
b++;
}
}
if (((l % 2 == 0) && (a % 2 || b % 2)))
{
printf("%d", -1);
return 0;
}
int left = 0, right = l - 1, nextAl = findNextR(pal, 'a', 0, l), nextAr = findNextL(pal, 'a', l - 1),
nextBl = findNextR(pal, 'b', 0, l), nextBr = findNextL(pal, 'b', l - 1), res = 0;
while (left < right)
{
if (pal[left] != pal[right])
{
nextAl = (nextAl < left) ? findNextR(pal, 'a', left, l) : nextAl;
nextBl = (nextBl < left) ? findNextR(pal, 'b', left, l) : nextBl;
nextAr = (nextAr > right) ? findNextL(pal, 'a', right) : nextAr;
nextBr = (nextBr > right) ? findNextL(pal, 'b', right) : nextBr;
if(pal[left] == 'a')
{
if (nextBl - left < right - nextAr)
{
pal[left] = 'b';
pal[nextBl] = 'a';
res += nextBl - left;
nextAr = nextBl > nextAr ? nextBl : nextAr;
nextBl = findNextR(pal, 'b', nextBl, l);
}
else
{
pal[right] = 'a';
pal[nextAr] = 'b';
res += right - nextAr;
nextBl = nextAr < nextBl ? nextAr : nextBl;
nextAr = findNextL(pal, 'a', nextAr);
}
}
else
{
if (nextAl - left < right - nextBr)
{
pal[left] = 'a';
pal[nextAl] = 'b';
res += nextAl - left;
nextBr = nextAl > nextBr ? nextAl : nextBr;
nextAl = findNextR(pal, 'a', nextAl, l);
}
else
{
pal[right] = 'b';
pal[nextBr] = 'a';
res += right - nextBr;
nextAl = nextBr < nextAl ? nextBr : nextAl;
nextBr = findNextL(pal, 'b', nextBr);
}
}
}
left++; right--;
}
printf("%d", res);
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 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; int findNextR(char* pal, char letter, int start, int len) { while (start < len && pal[start] != letter) { start++; } return start; } int findNextL(char* pal, char letter, int start) { while (start >= 0 && pal[start] != letter) { start--; } return start; } int main() { char* pal = new char[200001]; scanf("%s", pal); int l = strlen(pal); int a = 0, b = 0; for (int i = 0; i < l; i++) { if (pal[i] == 'a') { a++; } else { b++; } } if (((l % 2 == 0) && (a % 2 || b % 2))) { printf("%d", -1); return 0; } int left = 0, right = l - 1, nextAl = findNextR(pal, 'a', 0, l), nextAr = findNextL(pal, 'a', l - 1), nextBl = findNextR(pal, 'b', 0, l), nextBr = findNextL(pal, 'b', l - 1), res = 0; while (left < right) { if (pal[left] != pal[right]) { nextAl = (nextAl < left) ? findNextR(pal, 'a', left, l) : nextAl; nextBl = (nextBl < left) ? findNextR(pal, 'b', left, l) : nextBl; nextAr = (nextAr > right) ? findNextL(pal, 'a', right) : nextAr; nextBr = (nextBr > right) ? findNextL(pal, 'b', right) : nextBr; if(pal[left] == 'a') { if (nextBl - left < right - nextAr) { pal[left] = 'b'; pal[nextBl] = 'a'; res += nextBl - left; nextAr = nextBl > nextAr ? nextBl : nextAr; nextBl = findNextR(pal, 'b', nextBl, l); } else { pal[right] = 'a'; pal[nextAr] = 'b'; res += right - nextAr; nextBl = nextAr < nextBl ? nextAr : nextBl; nextAr = findNextL(pal, 'a', nextAr); } } else { if (nextAl - left < right - nextBr) { pal[left] = 'a'; pal[nextAl] = 'b'; res += nextAl - left; nextBr = nextAl > nextBr ? nextAl : nextBr; nextAl = findNextR(pal, 'a', nextAl, l); } else { pal[right] = 'b'; pal[nextBr] = 'a'; res += right - nextBr; nextAl = nextBr < nextAl ? nextBr : nextAl; nextBr = findNextL(pal, 'b', nextBr); } } } left++; right--; } printf("%d", res); return 0; } |
English