#include <cstdio> #include <cassert> #include <cstring> using namespace std; const int maxlen = 200002; char t[maxlen]; int len; int calc_result() { int l, r; int result = 0; l = 0; r = len-1; while(1) { int l2, r2; while(l < r && t[l] == t[r]) { ++l; --r; } if (l >= r) { return result; } l2 = l+1; r2 = r-1; while(l2 < r && t[l2] == t[l]) { ++l2; } while(l < r2 && t[r2] == t[r]) { --r2; } if (l2 >= r && l >= r2) { return -1; } if (l2-l < r-r2) { t[l2] = t[l]; result += l2-l; } else { t[r2] = t[r]; result += r-r2; } //printf("%s\n", t); ++l; --r; } } int main() { scanf("%s\n", t); len = strlen(t); printf("%d\n", calc_result()); 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 | #include <cstdio> #include <cassert> #include <cstring> using namespace std; const int maxlen = 200002; char t[maxlen]; int len; int calc_result() { int l, r; int result = 0; l = 0; r = len-1; while(1) { int l2, r2; while(l < r && t[l] == t[r]) { ++l; --r; } if (l >= r) { return result; } l2 = l+1; r2 = r-1; while(l2 < r && t[l2] == t[l]) { ++l2; } while(l < r2 && t[r2] == t[r]) { --r2; } if (l2 >= r && l >= r2) { return -1; } if (l2-l < r-r2) { t[l2] = t[l]; result += l2-l; } else { t[r2] = t[r]; result += r-r2; } //printf("%s\n", t); ++l; --r; } } int main() { scanf("%s\n", t); len = strlen(t); printf("%d\n", calc_result()); return 0; } |