博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
277 div2 C Palindrome Transformation
阅读量:6193 次
发布时间:2019-06-21

本文共 3432 字,大约阅读时间需要 11 分钟。

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-up->b a-down->z

我的思路是先把光标找到前半段的对应的位置之后找到要改变的字符的最大最小的位置(当已经是回文时记得输出0),定义一个sum整形

之后每当要改变的时候把最小的按键次数加在sum里,最后找到要改变的位置的最大最小值,先把sum+(max-min),之后加上光标位置到最大或最小值的最小值加到sum里最后输出sum;

基本就是先判断上下按的次数之后判断左右按下的次数下附代码

1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 #define M 100100 7 char s[M]; 8 9 int fun(char a,char b)10 {11 int x=a-b;12 if(x<0)13 x+=26;14 return x;15 }//判断改变字符最小的按键次数16 int main()17 {18 int n,p,i,j,sum,_min,_max;19 while(cin>>n>>p)20 {21 for(i=1;i<=n;i++)22 cin>>s[i];23 if(p>n/2)24 {25 if(n%2)26 {27 if(p==n/2+1);28 else29 p=n-p+1;30 }31 else32 p=n-p+1;33 }//判断光标在前半段的对应位置34 _min=M,_max=0,sum=0;//初始化35 for(i=1;i<=n/2;i++)36 {37 if(s[i]!=s[n-i+1])38 {39 sum+=min(fun(s[i],s[n-i+1]),fun(s[n-i+1],s[i]));40 if(i>_max)41 _max=i;42 if(i<_min)43 _min=i;44 }45 }//找到最小最大位置并且把上下按键次数输出46 if(sum==0)47 cout<
<
View Code

 

转载于:https://www.cnblogs.com/LQBZ/p/4267483.html

你可能感兴趣的文章
匿名内部类和内部类中的this
查看>>
[Python设计模式] 第27章 正则表达式——解释器模式
查看>>
ROS设备的性价比图
查看>>
日志分析方法
查看>>
Android TV 开发 (1)
查看>>
The POM for XXX is invalid, transitive dependencies (if any) will not be available解决方案
查看>>
让你的系统“坚挺不倒”的最后一个大招——「降级」
查看>>
处理linux下面的mysql乱码问题(下面的utf8换成gb2312也是可以的)
查看>>
Java常见设计模式之适配器模式
查看>>
免费 官方的ASP.NET MVC电子书-Professional ASP.NET MVC 1.0
查看>>
MS CRM 2011 RetrieveMultiple with JScript JQuery Silverlight LINQ FetchXML and QueryExpression
查看>>
Elasticsearch: Indexing SQL databases. The easy way
查看>>
应用开发框架之——插件、包
查看>>
SQL SERVER中强制类型转换cast和convert的区别
查看>>
备份数据表为insert 脚本
查看>>
ASP.NET MVC中检测浏览器版本并提示下载更新
查看>>
Online, Cheap -- and Elite
查看>>
exceptions.IOError: decoder jpeg not available
查看>>
【中文分词系列】 4. 基于双向LSTM的seq2seq字标注
查看>>
正则指引
查看>>