Gym - 100952C

C. Palindrome Again !!

 

Given string with N characters, your task is to transform it to a palindrome string. It's not as easy as you may think because there is a cost for this transformation!!

First you have to start from character at given position P. From your position you always have 2 options:

- You can move one step to the right or to the left, the cost of each movement is 1. Assume that the string is cyclic, this means if you move one step to the left you will be at position P-1 if P > 1 or at the last character if P = 1, and if you move one step to the right you will be at position P+1 if P < N or at first character if P = N.

- You can change the letter at your current position by replacing it with the next or previous one in the English alphabet (assume that the alphabet is also cyclic so ‘a’ is after ‘z’). The cost of each replacement is also 1.

You should repeat that until the transformation is finished and the string is palindrome. What is the minimum cost to do that?

Input

The first line contains the number of test cases T ( 1  ≤  T  ≤  100 ). Each test case contains 2 lines, the first line contains two integers ( 1  ≤  N  ≤  100,000) the length of string and ( 1  ≤  P  ≤  N ) the initial position. While the second line contains a string with exactly N alphabetical characters.

Output

For each test case output one line contains the minimum cost that is needed to change the string into a palindrome one.

Examples
input
1
8 3
aeabdaey
output
8
Note

start with P = 3 ae(a)bdaey, move right => aea(b)daey, change to next => aea(c)daey, change to next => aea(d)deay, move left => ae(a)ddeay, move left => a(e)addeay, move left => (a)eaddeay, change to previous => (z)eaddeay, change to previous => (y)eaddeay. This costs 8 (4 movements and 4 replacements)

 

模拟下,这题挺气的,比赛时没有考虑本身是回文字符串答案是0这种情况,赛后加上就AC了。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #define ll long long
 6 using namespace std;
 7 const int MAX = 1e5+10;
 8 char str[MAX];
 9 int main(){
10     int t;
11     cin>>t;
12     while(t--){
13         memset(str,0,sizeof(str));
14         int n,p;
15         cin>>n>>p;
16         p--;
17         cin>>str;
18         ll ans = 0;
19         int Min = MAX*10, Max = -1,flag = 0;
20         for(int i = 0; i < n/2; i ++){
21             if(str[i] != str[n-i-1]){
22                 int cnt = abs(str[i]-str[n-i-1]);
23                 ans+= min(cnt,26-cnt);
24                 Min = min(Min,i);
25                 Max = max(Max,i);
26                 flag = 1;
27             }
28         }
29         if(!flag){
30             cout << 0 << endl;
31             continue;
32         }
33         if(p >= n/2)p = n-p-1;
34         if(p <= Min){
35             cout <<ans+ Max-p << endl;
36         }else if(Min < p && p < Max){
37             cout << ans+Max-Min + min((p-Min),(Max-p)) << endl;;
38         }else if(p >= Max){
39             cout << ans+p-Min << endl;
40         }
41     }
42     return 0;
43 }

 

转载于:https://www.cnblogs.com/xingkongyihao/p/7197276.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值