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)

题目链接:http://codeforces.com/gym/100952/problem/C

题目大意:给出字符串a,将该字符串变成回文串!

分析:

  • 字符串向左边或右边移动一步(0往前移一格为n-1看成环),花费为1
  • 当前字母变为相邻字母,例如a -> b 或 a -> z, 花费为1

模拟此过程操作即可,代码给出了详细注释,一看就懂了!

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
}
int main()
{
int t;
t=read();
while(t--)
{
int len,p;
len=read();
p=read();
p--;//从零下标开始计数
string s;
cin>>s;
//移动距离
int minn=1e+;
int maxn=-1e+;
int ans=,flag=;
for(int i=,j=len-;i<len/;i++,j--)
{
if(s[i]!=s[j])
{
int a=min(s[i],s[j])-'a';
int b=max(s[i],s[j])-'a';
ans+=min(b-a,a+-b);//变换需要移动的最短距离
minn=min(minn,i);//满足条件最近的下标值
maxn=max(maxn,i);//满足条件最远的下标值
flag=;
}
}
if(!flag)//如果序列已经是回文序列
{
printf("0\n");
continue;
}
if(len%==&&p==len/)//奇数长度的回文序列,查找下标刚好是其中点时
{
ans+=abs(p-minn);//移动距离为其长度的一半
printf("%d\n",ans);
continue;
}
if(p>=len/)
p=len-p-;//这是一个回文序列,是对称的,所以我们采取序列号从小往大的排列方式
int c=abs(minn-p);
int d=abs(maxn-p);
ans+=min(c,d);//在变换过程中会忽略那些对称的点,所以这步是要加上那些忽略点的距离
ans+=(maxn-minn);//起始变换点与变换终点的距离,也就是移动距离
printf("%d\n",ans);
}
return ;
}

Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】的更多相关文章

  1. Gym 100952B&&2015 HIAST Collegiate Programming Contest B. New Job【模拟】

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

  2. Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】

    E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...

  3. Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】

    F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...

  4. Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】

    J. Polygons Intersection time limit per test:2 seconds memory limit per test:64 megabytes input:stan ...

  5. Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】

    I. Mancala time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ou ...

  6. 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 ...

  7. Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】

    G. The jar of divisors time limit per test:2 seconds memory limit per test:64 megabytes input:standa ...

  8. Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】

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

  9. 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 ...

随机推荐

  1. JavaScript类型比较

    JavaScript的类型 原始类型: number string boolean null undefined 对象类型: Object function Array Date ... 隐式转换 + ...

  2. iOS App稳定性指标及监测

    一个App的稳定性,主要决定于整体的系统架构设计,同时也不可忽略编程的细节,正所谓"千里之堤,溃于蚁穴",一旦考虑不周,看似无关紧要的代码片段可能会带来整体软件系统的崩溃.尤其因为 ...

  3. caffe CuDNN报错问题解决

    解决cudnn问题:Loaded runtime CuDNN library: 5005 (compatibility version 5000) but source was compiled wi ...

  4. TXDragon的大火题合集

    还有三个题没写,弃疗了 Solution

  5. OpenStack运维(一):OpenStack项目和用户

    1.添加项目 keystone tenant-create --name=demo [--description tenant-description --enable false] demo:项目名 ...

  6. Python练习100则--部分概念的没有做

    # coding = utf-8 import math, osfrom random import randint def Binary(): res = int(-1 / 2) res1 = in ...

  7. Java NIO (五) 管道 (Pipe)

    Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 如下图: 向管道写数据: 从管道读数据: 1. ...

  8. 怎么选择公司???MVC加jquery-easyui 后端工程师

    代码管理 Git 架构 MVC 这样的项目扩展性强,维护性强!!! 别很老的asp.net    不用框架!!!

  9. C/C++调用Golang 一

    C/C++调用Golang 一 (开发环境: 操作系统: windows 7 32位操作系统 C++: visual studio 2010 Golang:go version go1.9 windo ...

  10. Java_Date_02_截断日期到日

    oracle 的 trunc 函数能很方便的将日期截断.现在有个需求,需要用java实现与 oracle 的 trunc 函数 相同的功能. 1.需求:将日期截断到日 即 将格式为 2018-01-0 ...