Codeforces Gym100952 C. Palindrome Again !!-回文字符串 (2015 HIAST Collegiate Programming Contest)...

 
 
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
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)

 

题意就是把一个串变成回文的,从指定的位置开始,然后就是可以向字母表左右变换,一个环,就是a->b,或者a->z都是变化1次。每次不变化走过的步数也算在内,问最少要多少次使得这个串变成回文的。

mdzz,写这个题的时候,因为是从0开始存的这个串,所以开始的位置也要-1,但是我是智障啊,忘了。。。

代码的意思就是只改变串的前一半,后一半就不变化了,然后再把走的步数加上就可以。

举个例子,aeabdaey,只看前4个,a和y不一样,要变化,是从a->b->c这样正着往下变呢,还是倒着a->z->y这样变呢,就需要找这两个的变化次数最小的,所以min比较一下。

然后第二个字母是e和倒数第二个字母e一样,不变化,再往下一步,第三个字母a和倒数第三个字母a一样,不变化,第四个字母b和倒数第四个字母d不一样,要变化。

接下来是走的步数怎么算呢。因为是拿前一半比较的,如果指定的位置在后一半,就把这个位置改成前一半,所以if(m>=n/2)m=n-m-1;

然后比较一下,这个变化之后的位置如果是在需要改变的第一个字母的前面,那么走的步数就是从最后一个需要改变的字母的位置-指定的位置就可以。

如果在之间,就是最后一个要改变的字母的位置-第一个要改变的字母的位置,再比较一下,指定的位置是先往左走还是先往右走使得走的步数最少,就需要min比较一下。

如果指定的位置在最后一个要改变的字母的右边,那就是指定的位置-第一个要改变的字母的位置。

其实就是一个区间的问题,很好想的,我这个智障都想明白了(托脸~)。

 

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int INF=0x3f3f3f3f;
 4 const int N=1e5+10;
 5 char a[N];
 6 int main(){
 7     int t,n,m; 8 int minn,maxx,ans; 9 scanf("%d",&t); 10 while(t--){ 11 scanf("%d%d",&n,&m); 12 scanf("%s",a); 13 m--; //指定的位置-1。 14 minn=INF;maxx=-1;ans=0; 15 for(int i=0;i<n/2;i++){ //只要前一半。 16 if(a[i]!=a[n-i-1]){ 17 ans+=min(abs(a[i]-a[n-i-1]),26-abs(a[i]-a[n-i-1])); 18 minn=min(minn,i);maxx=max(maxx,i); 19  } 20  } 21 if(ans==0)printf("0\n"); 22 else{ 23 if(m>=n/2)m=n-m-1; //吧啦吧啦,看上面的解释。 24 if(m<=minn)ans+=maxx-m; 25 else if(m>minn&&m<maxx)ans+=maxx-minn+min((maxx-m),(m-minn)); 26 else if(m>=maxx)ans+=m-minn; 27 printf("%d\n",ans); 28  } 29  } 30 return 0; 31 }

 

 

 

 

 

转载于:https://www.cnblogs.com/ZERO-/p/9692957.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值