Gym 100952 C. Palindrome Again !!
1 second
64 megabytes
standard input
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?
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.
For each test case output one line contains the minimum cost that is needed to change the string into a palindrome one.
1
8 3
aeabdaey
8
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)题目大意,将字符串替换为回文串,且只能从指定的位置向左或者向右移动,每次替换字符需要消耗能量,移动一位也需要消耗能量,问最少消耗多少能量
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int t,n,p;
char s[];
int main()
{
scanf("%d",&t);
while(t--)
{
int left=,right=-,ans=;
bool flag=true;
scanf("%d%d%s",&n,&p,s);
p--;
for(int i=;i<n/;i++)
{
if(s[i]!=s[n--i])
{
int t=abs(s[i]-s[n-i-]);
ans+=min(t,-t);//对称位置替换需要的最少能量
left=min(i,left);
right=max(i,right);//left 和 right 记录回文串需要修改的区间
flag=false;
}
}
if(flag) {printf("0\n");continue;}
if(p>=n/) p=n-p-;
if(p<=left) printf("%d\n",ans+right-p);//从左向右移
else if(p>=right) printf("%d\n",ans+p-left);//从右向左移
else printf("%d\n",ans+right-left+min(p-left,right-p));//先移到最近一端在折返
}
return ;
}
Gym 100952 C. Palindrome Again !!的更多相关文章
- Gym 100952 H. Special Palindrome
http://codeforces.com/gym/100952/problem/H H. Special Palindrome time limit per test 1 second memory ...
- 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 ...
- codeforces gym 100952 A B C D E F G H I J
gym 100952 A #include <iostream> #include<cstdio> #include<cmath> #include<cstr ...
- 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 ...
- Gym 100952 F. Contestants Ranking
http://codeforces.com/gym/100952/problem/F F. Contestants Ranking time limit per test 1 second memor ...
- 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 ...
- Codeforces Gym 100570 E. Palindrome Query Manacher
E. Palindrome QueryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100570/pro ...
- Gym - 100570E:Palindrome Query (hash+BIT+二分维护回文串长度)
题意:给定字符串char[],以及Q个操作,操作有三种: 1:pos,chr:把pos位置的字符改为chr 2:pos:问以pos为中心的回文串长度为多长. 3:pos:问以pos,pos+1为中心的 ...
- 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 ...
随机推荐
- Benelux Algorithm Programming Contest 2014 Final(第二场)
B:Button Bashing You recently acquired a new microwave, and noticed that it provides a large number ...
- 负载均衡集群总结(Haproxy)
环境:Centos 6.9,Mysql 8.0 首先要先配置mysql主从复制集,可以参考我的上一篇>>Mysql 主从复制总结(详细) 我的主节点在(master):192.168.11 ...
- HDU-1035 Robot Motion 模拟问题(水题)
题目链接:https://cn.vjudge.net/problem/HDU-1035 水题 代码 #include <cstdio> #include <map> int h ...
- CF981C(菊花图)
题目描述 RAMESS知道很多关于树的问题(无循环的无向连通图)! 他创建了一个新的有用的树的划分,但他不知道如何构造它,所以他请求你的帮助! 划分是从树上的边中分裂出一些简单的路径,使得每个两条路径 ...
- 洛谷 P2015 二叉苹果树 && caioj1107 树形动态规划(TreeDP)2:二叉苹果树
这道题一开始是按照caioj上面的方法写的 (1)存储二叉树用结构体,记录左儿子和右儿子 (2)把边上的权值转化到点上,离根远的点上 (3)用记忆化搜索,枚举左右节点分别有多少个点,去递归 这种写法有 ...
- Unity C# 设计模式(三)工厂方法模式
定义: 定义一个创建对象的接口(父类),由子类决定需要实例化哪一个类. 这样,核心工厂类成为了一个抽象角色,不再负责产品的创建,仅提供具体工厂类所必须实现的接口,这样进一步抽象化的好处是使得工厂方法模 ...
- Myeclipse学习总结(4)——Eclipse常用开发插件
(1) AmaterasUML 介绍:Eclipse的UML插件,支持UML活动图,class图,sequence图,usecase图等:支持与Java class/interf ...
- ECNUOJ 2574 Principles of Compiler
Principles of Compiler Time Limit:1000MS Memory Limit:65536KBTotal Submit:473 Accepted:106 Descripti ...
- 【iOS开发-34】自己主动释放池@autoreleasepool的使用注意事项以及ARC机制——面试必考内容
自己主动释放池@autorelease面试频率可能会吧release还要高. (1)在自己主动释放池@autoreleasepool{}中alloc一个对象后(如p1).仍然须要用[p1 autore ...
- 解决Struts中文乱码问题总结
在进行struts开发的过程中.总也是出现非常多的乱码问题.但归根究竟,也仅仅是下面三种情况: ㈠页面显示中文乱码 ㈡传递參数中文乱码 ㈢国际化资源文件乱码 以下就这三中情况介绍怎么在详细项目 ...