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)

题意就是把一个串变成回文的,从指定的位置开始,然后就是可以向字母表左右变换,一个环,就是a->b,或者a->z都是变化1次。每次不变化走过的步数也算在内,问最少要多少次使得这个串变成回文的。

mdzz,写这个题的时候,因为是从0开始存的这个串,所以开始的位置也要-1,但是我是智障啊,忘了。。。

代码的意思就是只改变串的前一半,后一半就不变化了,然后再把走的步数加上就可以。

举个例子,aeabdaey,只看前4个,a和y不一样,要变化,是从a->b->c这样正着往下变呢,还是倒着a->z->y这样变呢,就需要找这两个的变化次数最小的,所以min比较一下。

然后第二个字母是e和倒数第二个字母e一样,不变化,再往下一步,第三个字母a和倒数第三个字母a一样,不变化,第四个字母b和倒数第四个字母d不一样,要变化。

接下来是走的步数怎么算呢。因为是拿前一半比较的,如果指定的位置在后一半,就把这个位置改成前一半,所以if(m>=n/2)m=n-m-1;

然后比较一下,这个变化之后的位置如果是在需要改变的第一个字母的前面,那么走的步数就是从最后一个需要改变的字母的位置-指定的位置就可以。

如果在之间,就是最后一个要改变的字母的位置-第一个要改变的字母的位置,再比较一下,指定的位置是先往左走还是先往右走使得走的步数最少,就需要min比较一下。

如果指定的位置在最后一个要改变的字母的右边,那就是指定的位置-第一个要改变的字母的位置。

其实就是一个区间的问题,很好想的,我这个智障都想明白了(托脸~)。

代码:

 1 #include<bits/stdc++.h>
2 using namespace std;
3 const int INF=0x3f3f3f3f;
4 const int N=1e5+10;
5 char a[N];
6 int main(){
7 int t,n,m;
8 int minn,maxx,ans;
9 scanf("%d",&t);
10 while(t--){
11 scanf("%d%d",&n,&m);
12 scanf("%s",a);
13 m--; //指定的位置-1。
14 minn=INF;maxx=-1;ans=0;
15 for(int i=0;i<n/2;i++){ //只要前一半。
16 if(a[i]!=a[n-i-1]){
17 ans+=min(abs(a[i]-a[n-i-1]),26-abs(a[i]-a[n-i-1]));
18 minn=min(minn,i);maxx=max(maxx,i);
19 }
20 }
21 if(ans==0)printf("0\n");
22 else{
23 if(m>=n/2)m=n-m-1; //吧啦吧啦,看上面的解释。
24 if(m<=minn)ans+=maxx-m;
25 else if(m>minn&&m<maxx)ans+=maxx-minn+min((maxx-m),(m-minn));
26 else if(m>=maxx)ans+=m-minn;
27 printf("%d\n",ans);
28 }
29 }
30 return 0;
31 }

Codeforces Gym100952 C. Palindrome Again !!-回文字符串 (2015 HIAST Collegiate Programming Contest)的更多相关文章

  1. Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】

    C. Palindrome Again !! time limit per test:1 second memory limit per test:64 megabytes input:standar ...

  2. Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】

    H. Special Palindrome time limit per test:1 second memory limit per test:64 megabytes input:standard ...

  3. Codeforces Gym100952 A.Who is the winner? (2015 HIAST Collegiate Programming Contest)

      A. Who is the winner?   time limit per test 1 second memory limit per test 64 megabytes input stan ...

  4. Codeforces Gym100952 B. New Job (2015 HIAST Collegiate Programming Contest)

      B. New Job   time limit per test 1 second memory limit per test 64 megabytes input standard input ...

  5. Codeforces Gym100952 D. Time to go back-杨辉三角处理组合数 (2015 HIAST Collegiate Programming Contest)

    D. Time to go back   time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Gym 100952A&&2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】

    A. Who is the winner? time limit per test:1 second memory limit per test:64 megabytes input:standard ...

  7. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. 125 Valid Palindrome 验证回文字符串

    给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...

  9. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. 《Cracking the Coding Interview》——第4章:树和图——题目6

    2014-03-19 04:16 题目:找出一棵二叉搜索树中的中序遍历后继节点,每个节点都有指针指向其父节点. 解法1:分两种情况:向下走时,先右后左:向上走时,先左后右.如果目标节点有右子树,就向右 ...

  2. 【vim环境配置】详细实录

    [写在前面] 以下的所有内容主要参照: https://github.com/yangyangwithgnu/use_vim_as_ide . 原blog作者写的非常用心,建议大家都去看看.(个人觉得 ...

  3. Python 3基础教程14-在文件尾部更新内容

    本文介绍在一个已经存在的文件尾部添加内容,还是用到write方法. 这里exampleFile.txt是前面文件创建的文件,里面有两行文字.

  4. pyinstaller打包自己的python程序

    使用Pyinstaller打包步骤如下 1. 安装pyinstaller pip install pyinstaller 查看安装的版本 pyinstaller --version 2. 给脚本加密 ...

  5. vmware设置静态ip(复制)

    一.安装好虚拟后在菜单栏选择编辑→ 虚拟网络编辑器,打开虚拟网络编辑器对话框,选择Vmnet8 Net网络连接方式,随意设置子网IP,点击NAT设置页面,查看子网掩码和网关,后面修改静态IP会用到. ...

  6. Spring 学习笔记(六)—— AOP的简单理解

    系统中的业务可以分为核心关注点和横切关注点. 核心关注点时业务处理的主要流程,而横切关注点是与核心业务无关但更为通用的业务. 各个横切关注点离散地穿插于核心业务之中,导致系统地每一个模块都与这些业务具 ...

  7. nyoj 题目44 子串和

    子串和 时间限制:5000 ms  |  内存限制:65535 KB 难度:3   描述 给定一整型数列{a1,a2...,an},找出连续非空子串{ax,ax+1,...,ay},使得该子序列的和最 ...

  8. 【noip2016d2t3】状压DP+巧妙优化

    题意可以简单这样考虑 给出n^2个集合(每个集合的元素不超过n),包含某个元素的集合至少有n个,选出最少的集合,使这些集合的并包含n个元素 n最大只有18 可以考虑状压n个元素,然后枚举n^2个集合 ...

  9. Lambda表达式使用2

    1.概述 本篇主要介绍lambda中常用的收集器,收集器的作用就是从数据流中生成需要的数据接口. 最常用的就是Collectors.toList(),只要将它传递给collect()函数,就能够使用它 ...

  10. linux系统初始化——文件系统初始化步骤

    linux文件系统初始化步骤 System V init启动过程 概括地讲,Linux/Unix系统一般有两种不同的初始化启动方式. 1) BSD system init 2) System V in ...