C. Palindrome Again !!
time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard output

Given string with N characters, your task is to transform it to a palindrome string. It's not as easy as you may think because there is a cost for this transformation!!

First you have to start from character at given position P. From your position you always have 2 options:

- You can move one step to the right or to the left, the cost of each movement is 1. Assume that the string is cyclic, this means if you move one step to the left you will be at position P-1 if P > 1 or at the last character if P = 1, and if you move one step to the right you will be at position P+1 if P < N or at first character if P = N.

- You can change the letter at your current position by replacing it with the next or previous one in the English alphabet (assume that the alphabet is also cyclic so ‘a’ is after ‘z’). The cost of each replacement is also 1.

You should repeat that until the transformation is finished and the string is palindrome. What is the minimum cost to do that?

Input

The first line contains the number of test cases T ( 1  ≤  T  ≤  100 ). Each test case contains 2 lines, the first line contains two integers ( 1  ≤  N  ≤  100,000) the length of string and ( 1  ≤  P  ≤  N ) the initial position. While the second line contains a string with exactly N alphabetical characters.

Output

For each test case output one line contains the minimum cost that is needed to change the string into a palindrome one.

Examples
Input
  1. 1
    8 3
    aeabdaey
Output
  1. 8
Note

start with P = 3 ae(a)bdaey, move right => aea(b)daey, change to next => aea(c)daey, change to next => aea(d)deay, move left => ae(a)ddeay, move left => a(e)addeay, move left => (a)eaddeay, change to previous => (z)eaddeay, change to previous => (y)eaddeay. This costs 8 (4 movements and 4 replacements)题目大意,将字符串替换为回文串,且只能从指定的位置向左或者向右移动,每次替换字符需要消耗能量,移动一位也需要消耗能量,问最少消耗多少能量

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #include <cmath>
  6. #include <map>
  7. #include <set>
  8. #include <vector>
  9. #include <algorithm>
  10. using namespace std;
  11. #define lowbit(x) (x&(-x))
  12. #define max(x,y) (x>y?x:y)
  13. #define min(x,y) (x<y?x:y)
  14. #define MAX 100000000000000000
  15. #define MOD 1000000007
  16. #define PI 3.141592653589793238462
  17. #define INF 0x3f3f3f3f3f
  18. #define mem(a) (memset(a,0,sizeof(a)))
  19. typedef long long ll;
  20. int t,n,p;
  21. char s[];
  22. int main()
  23. {
  24. scanf("%d",&t);
  25. while(t--)
  26. {
  27. int left=,right=-,ans=;
  28. bool flag=true;
  29. scanf("%d%d%s",&n,&p,s);
  30. p--;
  31. for(int i=;i<n/;i++)
  32. {
  33. if(s[i]!=s[n--i])
  34. {
  35. int t=abs(s[i]-s[n-i-]);
  36. ans+=min(t,-t);//对称位置替换需要的最少能量
  37. left=min(i,left);
  38. right=max(i,right);//left 和 right 记录回文串需要修改的区间
  39. flag=false;
  40. }
  41. }
  42. if(flag) {printf("0\n");continue;}
  43. if(p>=n/) p=n-p-;
  44. if(p<=left) printf("%d\n",ans+right-p);//从左向右移
  45. else if(p>=right) printf("%d\n",ans+p-left);//从右向左移
  46. else printf("%d\n",ans+right-left+min(p-left,right-p));//先移到最近一端在折返
  47. }
  48. return ;
  49. }

Gym 100952 C. Palindrome Again !!的更多相关文章

  1. Gym 100952 H. Special Palindrome

    http://codeforces.com/gym/100952/problem/H H. Special Palindrome time limit per test 1 second memory ...

  2. Gym 100952 D. Time to go back(杨辉三角形)

    D - Time to go back Gym - 100952D http://codeforces.com/gym/100952/problem/D D. Time to go back time ...

  3. codeforces gym 100952 A B C D E F G H I J

    gym 100952 A #include <iostream> #include<cstdio> #include<cmath> #include<cstr ...

  4. Gym 100952 G. The jar of divisors

    http://codeforces.com/gym/100952/problem/G G. The jar of divisors time limit per test 2 seconds memo ...

  5. Gym 100952 F. Contestants Ranking

    http://codeforces.com/gym/100952/problem/F F. Contestants Ranking time limit per test 1 second memor ...

  6. Gym 100952 D. Time to go back

    http://codeforces.com/gym/100952/problem/D D. Time to go back time limit per test 1 second memory li ...

  7. Codeforces Gym 100570 E. Palindrome Query Manacher

    E. Palindrome QueryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100570/pro ...

  8. Gym - 100570E:Palindrome Query (hash+BIT+二分维护回文串长度)

    题意:给定字符串char[],以及Q个操作,操作有三种: 1:pos,chr:把pos位置的字符改为chr 2:pos:问以pos为中心的回文串长度为多长. 3:pos:问以pos,pos+1为中心的 ...

  9. Gym 100952 A. Who is the winner?

    A. Who is the winner? time limit per test 1 second memory limit per test 64 megabytes input standard ...

随机推荐

  1. js一些常用方法

    string 增加 IsNullorEmpty : String.prototype.IsNullOrEmpty = function (r) {    if (r === undefined || ...

  2. java必会的英语单词

    Java开发常用英语单词表   第一章:public['pʌblik] 公共的,公用的static['stætik] 静的;静态的;静止的void:[vɔid] 空的main:[mein] 主要的 重 ...

  3. Xrdp - 通过Windows的RDP连接Linux远程桌面(Ubuntu/CentOS/Redhat 7)(转载)

            您多久访问一次Linux桌面? 您使用什么工具来访问远程桌面? Xrdp是一个开源工具,允许用户通过Windows RDP访问Linux远程桌面. 除了Windows RDP之外,xr ...

  4. bzoj 1588 [HNOI2002] 营业额统计 链表和Splay

    来自HNOI 2002营业额的统计一题,这题以前是用链表水过的,最近看见许多splay的题,赶紧张一下知识. 题目大意就是对于一个序列,输出每个元素与它之前元素的差的最小值的和.先说链表的方法吧. 大 ...

  5. HDU 4937 Lucky Number 搜索

    题意: 给你一个数,求在多少种不同的进制下这个数每一位都是3.4.5.6中的一个. 思路: 搜索.枚举这个数在任意进制下的表示,判断是否合法.当数字只有3.4.5.6时,必定有无穷种. 因为数字太大, ...

  6. HDU 3277 Marriage Match III

    Marriage Match III Time Limit: 4000ms Memory Limit: 32768KB This problem will be judged on HDU. Orig ...

  7. 金融项目java开发_BigDecimal(解决计算精度问题)

    当使用double进行商业运算时,double计算会丢失精度.可以使用BigDecimal进行计算. import java.math.BigDecimal; import org.junit.Tes ...

  8. Eclipse导出JavaDoc(并解决中文乱码问题)

    一. 使用Eclipse生成注释文档 使用eclipse生成文档(javadoc)主要有三种方法: 1,在项目列表中按右键,选择Export(导出),然后在Export(导出)对话框中选择java下的 ...

  9. C# DataTable中按字符串中的数字排序

    例如datatable中有一列是门牌号格式是xx-xx-xx,或字符串中含有汉字或其他符号等等,如何按照正确的数字顺序排序呢? 1.获得字符串中的数字. 2.在datatable中添加一列,类型是In ...

  10. berkeley db储存URL队列的简单实现增、删、查

     Berkeley DB(BDB)是一个高效的嵌入式数据库编程库,C语言.C++.Java.Perl.Python.Tcl以及其它非常多语言都有其相应的API. Berkeley DB能够保存随意 ...