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.


给你一个字符串以及它的长度。而且给你一个指针,指向字符串的初始位置,字符能够+或者-,字符a能够减到z,字符z能够加到a,指针能够向左或者向右移动,0号位置能够移动到最后的位置,最后的位置也能够移动到0号位置。

问最少多少部操作才干使得此字符串成为回文串。



依据指针起始位置。能够推断移动的时候在左、右的哪半部分移动,初始化求出字符须要变化的次数,然后贪心求出指针移动的次数。
//31 ms	 900 KB
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
char s[100007];
int x[100007],y[100007];
int main()
{
int len,p,count=0,num=0,j;
scanf("%d%d%s",&len,&p,s+1);
if(len==1)
{
printf("0\n");
return 0;
}
if(len&1)j=len/2+2;
else j=len/2+1;
for(int i=len/2; i>=0&&j<=len; i--,j++)
if(s[i]!=s[j])
{
int a=s[i]-'a';
int b=s[j]-'a';
int minn=min(a,b);
int maxx=max(a,b);
count+=min(maxx-minn,minn+26-maxx);
x[num]=i;
y[num++]=j;
}
sort(x,x+num);
sort(y,y+num);
if(p<=len/2)//假设指针在左部分
{
if(num!=0)
if(num==1)count+=fabs(x[0]-p);
else
{
int flag=0;
int aa=min(fabs(x[num-1]-p),fabs(p-x[0]));
if(x[0]<=p){flag++;count+=(p-x[0]);}//求最左面元素距离指针的距离
if(x[num-1]>p){flag++;count+=(x[num-1]-p);}//求最右面元素距离指针的距离
if(flag==2)count+=aa;//求最小距离
}
}
else//假设指针在右部分
{
if(num!=0)
if(num==1)count+=fabs(y[0]-p);
else
{
int flag=0;
int aa=min(fabs(y[num-1]-p),fabs(p-y[0]));
if(y[0]<=p){flag++;count+=(p-y[0]);}
if(y[num-1]>p){flag++;count+=(y[num-1]-p);}
if(flag==2)count+=aa;
}
}
printf("%d\n",count);
}

codeforces 486C Palindrome Transformation 贪心求构造回文的更多相关文章

  1. Codeforces 486C Palindrome Transformation(贪心)

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

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

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

  3. Codeforces Round 486C - Palindrome Transformation 贪心

    C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...

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

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

  5. USACO Prime Palindromes 构造回文数

    这道题目一点也不卡素数的判断 就是朴素的sqrt(n) 也不卡 所以~放心的用吧. 构造回文的时候看了HINT 其中是这么写的: Generate palindromes by combining d ...

  6. python练习 之 实践出真知 中心扩展法求最大回文子串 (leetcode题目)

    1 问题,给定一个字符串,求字符串中包含的最大回文子串,要求O复杂度小于n的平方. 首先需要解决奇数偶数的问题,办法是:插入’#‘,aba变成#a#b#a#,变成奇数个,aa变成#a#a#,变成奇数个 ...

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

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

  8. codeforces 897B Chtholly's request 偶数长度回文数

    codeforces 897B Chtholly's request 题目链接: http://codeforces.com/problemset/problem/897/B 思路: 暴力求出这100 ...

  9. hdu3613 Best Reward 扩展kmp or O(n)求最大回文子串

    /** 题目:hdu3613 Best Reward 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意:有一个字符串,把他切成两部分. 如果这部 ...

随机推荐

  1. LAMP总四部分

    第一部分 1. 安装mysqlcd /usr/local/src/ 免安装编译二进制的包wget http://syslab.comsenz.com/downloads/linux/mysql-5.1 ...

  2. BZOJ 3462 DZY Loves Math II ——动态规划 组合数

    好题. 首先发现$p$是互质的数. 然后我们要求$\sum_{i=1}^{k} pi*xi=n$的方案数. 然后由于$p$不相同,可以而$S$比较小,都是$S$的质因数 可以考虑围绕$S$进行动态规划 ...

  3. 个人环境搭建——搭建jenkins持续构建集成环境

    ---恢复内容开始--- 搭建jenkins持续构建集成环境  要搭建jenkins持续构建集成环境,首先要安装tomcat和JDK:   第一部分,基本说明:   敏捷(Agile) 在软件工程领域 ...

  4. serviceImpl中,方法加@Override注释后报错

    @Override public List<SysAdminMenu> getAdminMenusAll() { return sysAdminMenuMapper.getAdminMen ...

  5. codechef May Challenge 2016 FORESTGA: Forest Gathering 二分

    Description All submissions for this problem are available. Read problems statements in Mandarin Chi ...

  6. golang深坑记录

    go深坑:1.gin.context.JSON,如果没有make数组时,数组返回为null,make后,数组为[]2.json.Number转int64类型 datatemp.(json.Number ...

  7. 将扁平化的JSON属性转换为嵌套的JSON

    需要将如下JSON {"a":"a","b":"b","c.e":"e",&qu ...

  8. 配置微信api调扫码功能

    var url = encodeURIComponent(location.href.split('#')[0]); $.get(iapi+'/htweb/wx/getJsSdkSign?url='+ ...

  9. [LeetCode] Binary Search Tree Iterator 深度搜索

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  10. EOJ Monthly 2017.12 A B C D

    // 因为是中文题面就偷一次懒不写题意啦QAQ // 各种大作业然后又要期末还不知道什么时候能补题QAQ A. 唐纳德先生和假骰子 直接模拟 #include <bits/stdc++.h> ...