277 div2 C Palindrome Transformation

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<iostream>
 2 #include<string>
 3 #include<cmath>
 4 #include<cstdlib>
 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                 else
29                     p=n-p+1;
30             }
31             else
32                 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<<sum<<endl;//当sum==0时代表已经是回文不需要处理
48         else
49         {
50             sum+=_max-_min+min(abs(_max-p),abs(_min-p));//sum加上最小的左右移动位置
51             cout<<sum<<endl;
52         }
53     }
54     return 0;
55 }
View Code

 

posted @ 2015-02-02 12:09  乱齐八糟  阅读(308)  评论(0编辑  收藏  举报