Gym 100952 C. Palindrome Again !!

C. Palindrome Again !!
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

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)题目大意,将字符串替换为回文串,且只能从指定的位置向左或者向右移动,每次替换字符需要消耗能量,移动一位也需要消耗能量,问最少消耗多少能量

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int t,n,p;
char s[100006];
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        int left=100006,right=-1,ans=0;
        bool flag=true;
        scanf("%d%d%s",&n,&p,s);
        p--;
        for(int i=0;i<n/2;i++)
        {
            if(s[i]!=s[n-1-i])
            {
                int t=abs(s[i]-s[n-i-1]);
                ans+=min(t,26-t);//对称位置替换需要的最少能量
                left=min(i,left);
                right=max(i,right);//left 和 right 记录回文串需要修改的区间
                flag=false;
            }
        }
        if(flag) {printf("0\n");continue;}
        if(p>=n/2) p=n-p-1;
        if(p<=left) printf("%d\n",ans+right-p);//从左向右移
        else if(p>=right) printf("%d\n",ans+p-left);//从右向左移
        else printf("%d\n",ans+right-left+min(p-left,right-p));//先移到最近一端在折返
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7199165.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值