time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more
beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.

There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n,
the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or
to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n,
the cursor appears at the beginning of the string).

When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z'
follows 'a'). The same holds when he presses the down arrow key.

Initially, the text cursor is at position p.

Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?

Input

The first line contains two space-separated integers n (1 ≤ n ≤ 105)
and p (1 ≤ p ≤ n), the length of Nam's
string and the initial position of the text cursor.

The next line contains n lowercase characters of Nam's string.

Output

Print the minimum number of presses needed to change string into a palindrome.

Sample test(s)
input
8 3
aeabcaez
output
6
Note

A string is a palindrome if it reads the same forward or reversed.

In the sample test, initial Nam's string is:  (cursor
position is shown bold).

In optimal solution, Nam may do 6 following steps:

The result, ,
is now a palindrome.

解题思路:贪心。仅仅考虑一边的情况就可以,以左半为例。不断贪心考虑pos离哪边的不匹配边界近,如此往复就可以。

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath> using namespace std; int main(){
int n, pos;
string s;
// freopen("in.txt", "r", stdin);
while(scanf("%d%d", &n, &pos)!=EOF){
pos --;
cin>>s;
int l = 0, r = n/2 - 1; if(n/2 <= pos) pos = n - 1 - pos; while(l < n/2 && s[l] == s[n - 1 - l]) l++;
while(r >= 0 && s[r] == s[n - 1 - r]) r--; int ans = 0; for(int i=l; i<=r; i++){
int temp = abs(s[i] - s[n-1-i]);
ans += min(temp, 26 - temp);
}
if(l < r)
ans += min(abs(pos - l), abs(r - pos)) + r - l;
else if(l == r)
ans += abs(pos - r);
cout<<ans<<endl;
}
return 0;
}

最新文章

  1. ASP.NET通过递归添加树(Treeview)
  2. linux下文件搜索命令学习笔记
  3. AutoMagic
  4. XMLHttpRequest
  5. ASP.NET MVC 随想录——探索ASP.NET Identity 身份验证和基于角色的授权,中级篇
  6. Beta版本项目展示要求
  7. word2007无法执行语言识别
  8. 获取SqlServer2005表结构(字段,主键,外键,递增,描述)
  9. Windows Phone 7 中拷贝文件到独立存储
  10. Java 判断是否为汉字 判断是否为乱码 判断字符串是否为双整型数字 整数 数字
  11. 解决Hadoop-Eclipse-Plugin放在Plugin目录下没反应的问题
  12. Java中的多线程Demo
  13. AspNet Core 下利用普罗米修斯+Grafana构建Metrics和服务器性能的监控 (无心打造文字不喜勿喷谢谢!)
  14. MySQL实现批量检查表并进行repair与optimize的方法
  15. python csv与字典操作
  16. [IOI2000] 邮局
  17. LOJ 6277-6280 数列分块入门 1-4
  18. Python2018-字符串中字符个数统计
  19. 学习笔记TF037:实现强化学习策略网络
  20. java 反射 子类泛型的class

热门文章

  1. python爬虫---从零开始(五)pyQuery库
  2. postman使用--断言
  3. 下载GitHub上的dnSpy源码
  4. Go:json(序列化、反序列化)
  5. maven打包oracle jdbc驱动
  6. 07 mongodb
  7. win10修改文件管理器默认打开我的电脑
  8. LeetCode(71) Simplify Path
  9. sysctl.conf文件配置详解
  10. ExtJs 滚动条问题