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. 【bzoj1266】[AHOI2006]上学路线route 最短路+最小割

    题目描述 可可和卡卡家住合肥市的东郊,每天上学他们都要转车多次才能到达市区西端的学校.直到有一天他们两人参加了学校的信息学奥林匹克竞赛小组才发现每天上学的乘车路线不一定是最优的. 可可:“很可能我们在 ...

  2. POJ 1941 The Sierpinski Fractal ——模拟

    只需要开一个数组,记录一下这个图形. 通过一番计算,发现最大的面积大约是2k*2k的 然后递归下去染三角形. 需要计算出左上角的坐标. 然后输出的时候需要记录一下每一行最远延伸的地方,防止行末空格过多 ...

  3. BZOJ1491 [NOI2007]社交网络 【floyd】

    题目 在社交网络(socialnetwork)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题. 在一个社交圈子里有n个人,人与人之间有不同程度的关系.我们将这个关系网络对应到一 ...

  4. BZOJ3998 [TJOI2015]弦论 【后缀自动机】

    题目 对于一个给定长度为N的字符串,求它的第K小子串是什么. 输入格式 第一行是一个仅由小写英文字母构成的字符串S 第二行为两个整数T和K,T为0则表示不同位置的相同子串算作一个.T=1则表示不同位置 ...

  5. 从jekyll转向hexo

    当年选择jekyll是因为看中了HCZ Material theme这个主题,折腾了很久才把博客搭建好,后来周边人准备些博客的时候已经不推荐使用jekyll了,推荐hexo给好几个人,不用他们折腾,( ...

  6. 【BZOJ3895】取石子(博弈,记忆化搜索)

    题意: Alice和Bob两个好朋含友又开始玩取石子了.游戏开始时,有N堆石子排成一排,然后他们轮流操作(Alice先手),每次操作时从下面的规则中任选一个:1:从某堆石子中取走一个2:合并任意两堆石 ...

  7. 十步优化SQL Server中的数据访问

    原文发布时间为:2011-02-24 -- 来源于本人的百度文章 [由搬家工具导入] 转载:http://tech.it168.com/a2009/1125/814/000000814758_all. ...

  8. DOS头结构

    DOS头结构typedef struct _IMAGE_DOS_HEADER {                 // DOS .EXE header   +0h WORD   e_magic;    ...

  9. django Modelform 使用

    前言: 为什么要用form去验证呢? 我们提交的是form表单,在看前端源码时如果检查到POST URL及我们提交的字段,如果没有验证我们是否可以直接POST数据到URL,后台并没有进行校验,直接处理 ...

  10. poj 2892(二分+树状数组)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7749   Accepted: 3195 D ...