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
| #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<climits> #include<stack> #include<vector> #include<queue> #include<set> #include<map>
#include<cstdio> #include<string> #define up(i,a,b) for(int i=a;i<b;i++) #define dw(i,a,b) for(int i=a;i>b;i--) #define upd(i,a,b) for(int i=a;i<=b;i++) #define dwd(i,a,b) for(int i=a;i>=b;i--)
typedef long long ll; const double esp = 1e-6; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int inf = 1e9; using namespace std; int read() { char ch = getchar(); int x = 0, f = 1; while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } typedef pair<int, int> pir; #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define lrt root<<1 #define rrt root<<1|1 int T; int n,p; string s; string s1; int main() { T = read(); while (T--) { n = read(); p = read(); s = " "; cin >> s1; s += s1; int mid = n / 2; bool flag = true; dwd(i, mid, 1) { if(s[i]!=s[n-i+1]) { flag = false; break; } } if (flag)cout << 0 << endl; else { int ans = 0; int lf = mid+1; int rt = 0; dwd(i, mid, 1) { if (s[i] != s[n - i + 1]) { ans += min(abs(s[i] - s[n - i + 1]), 26 - abs(s[i] - s[n - i + 1])); lf = min(lf, i); rt = max(rt, i); } } if (n % 2)if (p == mid + 1) { cout << ans + p - lf<<endl; continue; } if (n % 2 == 0)if (p == mid) { cout << ans + p-lf<< endl; continue; } if (p > mid)p = n - p+1; if (p <= lf) { cout << ans + rt - p << endl; continue; } if (p >= rt) { cout << ans + p-lf << endl; continue; } int tempans = 1e9; tempans = min(n - p + lf, tempans); tempans = min(rt - p + rt - lf,tempans); tempans = min(tempans, p - lf + rt - lf); tempans = min(tempans, p + n - rt); cout << tempans + ans << endl; } } return 0; }
|