C. Palindrome Transformation
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.

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
const int maxn=+;
int main()
{
sspeed;
int n,pos;
string s;
cin>>n>>pos;
pos--;
if(pos>n/-)
pos=n-pos-;
cin>>s;
int ans=;
int l=,r=n/-;
while(l<n/&&s[l]==s[n-l-])
l++;
while(r>=&&s[r]==s[n-r-])
r--;
//cout<<l<<" "<<r<<endl;
if(l<=r){
for(int i=l;i<=r;i++)
{
int temp=abs(s[i]-s[n-i-]);
ans+=min(temp,-temp);
//cout<<ans<<endl;
}
ans=ans+min(abs(pos-r),abs(pos-l))+abs(l-r);
cout<<ans<<endl;
}
else
cout<<ans<<endl;
return ;
}

Codeforces Round 486C - Palindrome Transformation 贪心的更多相关文章

  1. Codeforces 486C Palindrome Transformation(贪心)

    题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...

  2. codeforces 486C Palindrome Transformation 贪心求构造回文

    点击打开链接 C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes ...

  3. CodeForces 486C Palindrome Transformation 贪心+抽象问题本质

    题目:戳我 题意:给定长度为n的字符串,给定初始光标位置p,支持4种操作,left,right移动光标指向,up,down,改变当前光标指向的字符,输出最少的操作使得字符串为回文. 分析:只关注字符串 ...

  4. Codeforces Round #277 (Div. 2)C.Palindrome Transformation 贪心

    C. Palindrome Transformation     Nam is playing with a string on his computer. The string consists o ...

  5. codeforces 486C. Palindrome Transformation 解题报告

    题目链接:http://codeforces.com/problemset/problem/486/C 题目意思:给出一个含有 n 个小写字母的字符串 s 和指针初始化的位置(指向s的某个字符).可以 ...

  6. Educational Codeforces Round 64 -B(贪心)

    题目链接:https://codeforces.com/contest/1156/problem/B 题意:给一段字符串,通过变换顺序使得该字符串不包含为位置上相邻且在字母表上也相邻的情况,并输出. ...

  7. Educational Codeforces Round 20 C 数学/贪心/构造

    C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. Codeforces Round #295 D. Cubes [贪心 set map]

    传送门 D. Cubes time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  9. C. Arthur and Table(Codeforces Round #311 (Div. 2) 贪心)

    C. Arthur and Table time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. Android Bander设计与实现 - 设计

    Binder Android IPC Linux 内核 驱动 摘要 Binder是Android系统进程间通信(IPC)方式之一.Linux已经拥有管道,system V IPC,socket等IPC ...

  2. Linux学习笔记-文件系统和基本命令

    目录 分区设备文件名 分区 挂载 文件目录 文件处理命令 目录处理命令 硬件设备文件名 IDE硬盘 /dev/hd[a-d] USB硬盘 /dev/sd[a-p] 光驱 /dev/cdrom或者/de ...

  3. php CI框架

    CodeIgniter 是一个小巧但功能强大的 PHP 框架,作为一个简单而“优雅”的工具包,它可以为 PHP 程序员建立功能完善的 Web 应用程序.如果你是一个使用共享主机,并且为客户所要求的期限 ...

  4. ActiveMQ之VirtualTopic是什么?

    一句话总结: VirtualTopic是为了解决持久化模式下多消费端同时接收同一条消息的问题.   想象这样一个场景:   生产端产生了一笔订单,作为消息MessageOrder发了出去. 这笔订单既 ...

  5. ansible的几点记录

    user模块的password加密 ansible中user模块的password需要传入加密数据,可以通过这种方式快速加密: To get a sha512 password hash (rando ...

  6. JsRender 前端渲染模板常用API学习

    JsRender 常用API 1. $.templates() $.templates()方法是用来注册或编译模板的,使用的情况有以下几种. 把html字符串编译编译成模板 获取使用script标签声 ...

  7. 关于UrlEncode 一团乱麻的问题,后续彻底理解。Java中的 URLEncoder 与 URLDecoder无bug

    很多开放平台都是小白开发的,对这个urlencode理解的不到位,他们总是认为java官方的urlencode有bug,需要 URLEncoder.encode("Hello World&q ...

  8. hdu 5943(素数间隔+二分图匹配)

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  9. Oracle约束

    1.非空约束 DROP TABLE member PURGE; CREATE TABLE member( mid NUMBER, name ) NOT NULL ); 2.唯一约束 DROP TABL ...

  10. overlay实现容器跨主机通信

    本节内容: Docker容器跨主机通信方案 环境信息 升级内核 安装docker 防火墙设置和开启内核转发 安装启动consul 启动Docker 创建overlay network 创建容器 测试容 ...