Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】
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?
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)
题目链接:http://codeforces.com/gym/100952/problem/C
题目大意:给出字符串a,将该字符串变成回文串!
分析:
字符串向左边或右边移动一步(0往前移一格为n-1看成环),花费为1
当前字母变为相邻字母,例如a -> b 或 a -> z, 花费为1
模拟此过程操作即可,代码给出了详细注释,一看就懂了!
下面给出AC代码:
- #include <bits/stdc++.h>
- using namespace std;
- inline int read()
- {
- int x=,f=;
- char ch=getchar();
- while(ch<''||ch>'')
- {
- if(ch=='-')
- f=-;
- ch=getchar();
- }
- while(ch>=''&&ch<='')
- {
- x=x*+ch-'';
- ch=getchar();
- }
- return x*f;
- }
- inline void write(int x)
- {
- if(x<)
- {
- putchar('-');
- x=-x;
- }
- if(x>)
- write(x/);
- putchar(x%+'');
- }
- int main()
- {
- int t;
- t=read();
- while(t--)
- {
- int len,p;
- len=read();
- p=read();
- p--;//从零下标开始计数
- string s;
- cin>>s;
- //移动距离
- int minn=1e+;
- int maxn=-1e+;
- int ans=,flag=;
- for(int i=,j=len-;i<len/;i++,j--)
- {
- if(s[i]!=s[j])
- {
- int a=min(s[i],s[j])-'a';
- int b=max(s[i],s[j])-'a';
- ans+=min(b-a,a+-b);//变换需要移动的最短距离
- minn=min(minn,i);//满足条件最近的下标值
- maxn=max(maxn,i);//满足条件最远的下标值
- flag=;
- }
- }
- if(!flag)//如果序列已经是回文序列
- {
- printf("0\n");
- continue;
- }
- if(len%==&&p==len/)//奇数长度的回文序列,查找下标刚好是其中点时
- {
- ans+=abs(p-minn);//移动距离为其长度的一半
- printf("%d\n",ans);
- continue;
- }
- if(p>=len/)
- p=len-p-;//这是一个回文序列,是对称的,所以我们采取序列号从小往大的排列方式
- int c=abs(minn-p);
- int d=abs(maxn-p);
- ans+=min(c,d);//在变换过程中会忽略那些对称的点,所以这步是要加上那些忽略点的距离
- ans+=(maxn-minn);//起始变换点与变换终点的距离,也就是移动距离
- printf("%d\n",ans);
- }
- return ;
- }
Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】的更多相关文章
- Gym 100952B&&2015 HIAST Collegiate Programming Contest B. New Job【模拟】
B. New Job time limit per test:1 second memory limit per test:64 megabytes input:standard input outp ...
- Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...
- Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...
- Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】
J. Polygons Intersection time limit per test:2 seconds memory limit per test:64 megabytes input:stan ...
- Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】
I. Mancala time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ou ...
- Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】
H. Special Palindrome time limit per test:1 second memory limit per test:64 megabytes input:standard ...
- Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】
G. The jar of divisors time limit per test:2 seconds memory limit per test:64 megabytes input:standa ...
- Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】
D. Time to go back time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- Gym 100952A&&2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】
A. Who is the winner? time limit per test:1 second memory limit per test:64 megabytes input:standard ...
随机推荐
- 【HTML】HTML基础知识
<!DOCTYPE html>表示HTML5文档申明,不区别大小写,通常这么写. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 ...
- Simple Games Using SpriteKit
p.p1 { margin: 0.0px 0.0px 12.0px 0.0px; line-height: 14.0px; font: 12.0px Times; color: #000000 } s ...
- Java I/O---Properties类(持久化键值对)
1.Properties类简介 Properties类(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这 ...
- python模拟shell执行脚本
工作时候需要模拟shell来执行任务,借助包paramkio import paramiko class ShellExec(object): host = '127.0.0.1' port = 36 ...
- 598. Range Addition II
Given an m * n matrixMinitialized with all0's and several update operations. Operations are represen ...
- 利用generator自动生成model(实体)、dao(接口)、mapper(映射)
1 在MySQL数据库中创建相应的表 /* Navicat MySQL Data Transfer Source Server : 虚拟机_zeus01 Source Server Version : ...
- badboy 录制脚本并并发脚本
很久没有研究过接口相关的工具了,一个偶然的机会听说了 badboy,可以录制jemter脚本, 查了资料 还可以并发,于是乎,实践才知道. http://www.badboy.com.au/ 官网,我 ...
- 利用USearch去除嵌合体(chimeras)
嵌合体序列指在pcr过程中,两条不同的序列产生杂交扩增的序列,属于人工污染,在ITS和16S分析中,应该首先去除,USearch提供去除嵌合体的功能 usearch -uchime_ref reads ...
- Python 词云分析周杰伦《晴天》
一.前言满天星辰的夜晚,他们相遇了...夏天的时候,她慢慢的接近他,关心他,为他付出一切:秋天的时候,两个人终於如愿的在一起,分享一切快乐的时光但终究是快乐时光短暂,因为杰伦必须出国深造,两人面临了要 ...
- JavaScript Dom入门
好像代码太杂了,博客园里跑不起来,单独复制到html中本地测试都是没有问题的. JavaScript JavaScript 是属于 web 的语言,它适用于 PC.笔记本电脑.平板电脑和移动电话. J ...