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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFF 204800

int main() {
    char *pal, *i, *j, t;
    int l,a,b,s,k;
    pal = (char *)malloc(BUFF);
    fgets(pal, BUFF, stdin);
    l = strlen(pal);
    while (pal[l-1] < 'a') {
        pal[l-1] = '\0';
        l--;
    }
    
    i = pal;
    j = &(pal[l-1]);

    if (l % 2 == 0) {
        a = b = 0;
        while (*i) {
            if (*i == 'a') a++; else b++;
            i++;
        }
        if (a % 2 || b % 2) {
            puts("-1");
            free(pal);
            return 0;
        }
    }

    i = pal;
    s = 0;

    while (i < j) {
        k = 0;
        while (*i != *j) {
            k++;
            s++;
            if (*i != *(i+k)) {
                t = *(i+k);
                *(i+k) = *i;
                *i = t;
            } else if (*j != *(j-k)) {
                t = *(j-k);
                *(j-k) = *j;
                *j = t;
            }
        }

        i++;j--;

    }

    printf("%d\n", s);
    
    return 0;
}