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
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX(a,b) (a > b ? a : b)

char *
reverse(char *s)
{
    int len = strlen(s);
    char tmp = 0;

    int i;
    for (i = 0; i < len / 2; i++) {
        //printf("%d %c\n", len - i - 1, s[len - i - 1]);
        tmp = s[len - i - 1];
        s[len - i - 1] = s[i];
        s[i] = tmp;
    }

    return s;
}

char
*substr(char *s, int start, int len)
{
    char *tmp = (char *)malloc(strlen(s) + 1);
    int i;

    for (i = 0; i < len; i++) {
        tmp[i] = s[start + i];
    }

    tmp[i] = 0;

    return tmp;
}

void
swap(char *s, int i)
{
    char tmp = s[i];
    s[i] = s[i + 1];
    s[i + 1] = tmp;
}

int
isPalindrom(char *s)
{
    if(strlen(s) == 1) return 0;

    char *ptr = s;
    int a = 0;
    int b = 0;

    while (*s) {
        if (*s == 'a') a++;
        if (*s == 'b') b++;
        s++;
    }

    //printf("%d %d\n", a, b);
    if (a%2 && b%2) return 0;
    return 1;
}

int
main(void)
{
    // for (int i = 0; i < 5000; i++) {
    //     printf("9");
    // }

    //printf("\n");
    char word[200001];

    scanf("%s", word);

    if (!isPalindrom(word)) {
        printf("-1\n");
        return 0;
    }

    int len = strlen(word);

    char *a,*b;

    int left = 0;
    int right = strlen(word) - 1;
    int c = 0;



    while (left < right) {
        //printf("%d %d\n", left, right);
        int l = left;
        int r = right;

        while(word[left] != word[r]) {
            r--;
        }

        //printf("l: %d r: %d\n", l, r);

        if (left == r) {
            swap(word, l);
            c++;
        } else {
            for (int i = r;i<right; i++) {
                //printf("i: %d r: %d\n", i, r);
                swap(word, i);
                c++;
            }
        }

        //printf("%s\n", word);
        left++;
        right--;
    }

    printf("%d\n", c);


    return 0;
}