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. C#中MessageBox用法大全(转)

    我们在程序中经常会用到MessageBox. MessageBox.Show()共有21中重载方法.现将其常见用法总结如下: 1.MessageBox.Show("Hello~~~~&quo ...

  2. 如何录屏制作gif图片

    LICEcap简介: LICEcap是一款屏幕录制制作gif图片的工具,支持导出 GIF 动画图片格式,轻量级.使用简单,录制过程中可以随意改变录屏范围. LICEcap 直观易用,功能灵活,支持 W ...

  3. 基于阿里云的MQTT远程控制

    好久没有写博客了,眼看自己的项目就要快做完了,先分享一下基于MQTT的远程控制,自己买了一个阿里的云端,然后在云端上安装了一个MQTT服务器,其实是一不小心买了两个,所以准备贡献出来一个供大家使用, ...

  4. elasticsearch 源码本地环境搭建

    elasticsearch6.0.0 源码本地环境搭建步骤如下: 1.资源准备 ElasicSearch版本:6.0.0: https://github.com/elastic/elasticsear ...

  5. ActiveMQ (三) 讯息传送机制以及ACK机制

    详析请看如下博客: http://blog.csdn.net/lulongzhou_llz/article/details/42270113 后续再做整理.

  6. MySql Schema 优化

    MySQL Schema 优化:   1.保证你的数据库的整洁性.   2.归档老数据 — 删除查询中检索或返回的多余的行   3.在数据上加上索引.   4.不要过度使用索引,评估你的查询.   5 ...

  7. 用python爬整本小说写入txt文件

    没太完善,但是可以爬下整本小说.日后会写入数据库,注释不要太在意,都是调试的.入库估计这周之后,这次爬的是笔趣阁的第1150本书,大家只要可以改get_txt()里数字就行,查到自己要看哪本书一改就可 ...

  8. Go 语言编写单元测试

    吾尝终日而思矣,不如须臾之所学也:吾尝跂而望矣,不如登高之博见也.登高而招,臂非加长也,而见者远:顺风而呼,声非加疾也,而闻者彰.假舆马者,非利足也,而致千里:假舟楫者,非能水也,而绝江河.君子生非异 ...

  9. 用元类和__getattribute__改变类属性的读取方式

    首先,需要知道两点: 类本身是type类的实例 __getattribute__ 可以改变类实例的属性的读取方式(http://www.cnblogs.com/blackmatrix/p/568148 ...

  10. Python的__getattr__和__getattribute__

    __getattr____getattr__在当前主流的Python版本中都可用,重载__getattr__方法对类及其实例未定义的属性有效.也就属性是说,如果访问的属性存在,就不会调用__getat ...