• Gym


    C. Palindrome Again !!

     

    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
    8 3
    aeabdaey
    output
    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)

    模拟下,这题挺气的,比赛时没有考虑本身是回文字符串答案是0这种情况,赛后加上就AC了。

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <algorithm>
     5 #define ll long long
     6 using namespace std;
     7 const int MAX = 1e5+10;
     8 char str[MAX];
     9 int main(){
    10     int t;
    11     cin>>t;
    12     while(t--){
    13         memset(str,0,sizeof(str));
    14         int n,p;
    15         cin>>n>>p;
    16         p--;
    17         cin>>str;
    18         ll ans = 0;
    19         int Min = MAX*10, Max = -1,flag = 0;
    20         for(int i = 0; i < n/2; i ++){
    21             if(str[i] != str[n-i-1]){
    22                 int cnt = abs(str[i]-str[n-i-1]);
    23                 ans+= min(cnt,26-cnt);
    24                 Min = min(Min,i);
    25                 Max = max(Max,i);
    26                 flag = 1;
    27             }
    28         }
    29         if(!flag){
    30             cout << 0 << endl;
    31             continue;
    32         }
    33         if(p >= n/2)p = n-p-1;
    34         if(p <= Min){
    35             cout <<ans+ Max-p << endl;
    36         }else if(Min < p && p < Max){
    37             cout << ans+Max-Min + min((p-Min),(Max-p)) << endl;;
    38         }else if(p >= Max){
    39             cout << ans+p-Min << endl;
    40         }
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    Python3 CGI编程实现教程
    SSL密钥协商过程分析
    浏览器同源策略理解
    Python3+selenium 报错处理:“selenium.common.exceptions.NoAlertPresentException: Message: No alert is active”
    Python3 try-except、raise和assert解析
    计算机视觉常见技术(待理解)
    中国大学MOOC-陈越、何钦铭-数据结构-2017春
    Coursera机器学习+deeplearning.ai+斯坦福CS231n
    总结一些机器视觉库
    git rebase 多分支操作
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7197276.html
Copyright © 2020-2023  润新知