codeforces 486C Palindrome Transformation 贪心求构造回文
1 second
256 megabytes
standard input
standard output
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more
beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n,
the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or
to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n,
the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z'
follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
The first line contains two space-separated integers n (1 ≤ n ≤ 105)
and p (1 ≤ p ≤ n), the length of Nam's
string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
Print the minimum number of presses needed to change string into a palindrome.
8 3
aeabcaez
6
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: (cursor
position is shown bold).
In optimal solution, Nam may do 6 following steps:
The result, ,
is now a palindrome.
问最少多少部操作才干使得此字符串成为回文串。
//31 ms 900 KB
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
char s[100007];
int x[100007],y[100007];
int main()
{
int len,p,count=0,num=0,j;
scanf("%d%d%s",&len,&p,s+1);
if(len==1)
{
printf("0\n");
return 0;
}
if(len&1)j=len/2+2;
else j=len/2+1;
for(int i=len/2; i>=0&&j<=len; i--,j++)
if(s[i]!=s[j])
{
int a=s[i]-'a';
int b=s[j]-'a';
int minn=min(a,b);
int maxx=max(a,b);
count+=min(maxx-minn,minn+26-maxx);
x[num]=i;
y[num++]=j;
}
sort(x,x+num);
sort(y,y+num);
if(p<=len/2)//假设指针在左部分
{
if(num!=0)
if(num==1)count+=fabs(x[0]-p);
else
{
int flag=0;
int aa=min(fabs(x[num-1]-p),fabs(p-x[0]));
if(x[0]<=p){flag++;count+=(p-x[0]);}//求最左面元素距离指针的距离
if(x[num-1]>p){flag++;count+=(x[num-1]-p);}//求最右面元素距离指针的距离
if(flag==2)count+=aa;//求最小距离
}
}
else//假设指针在右部分
{
if(num!=0)
if(num==1)count+=fabs(y[0]-p);
else
{
int flag=0;
int aa=min(fabs(y[num-1]-p),fabs(p-y[0]));
if(y[0]<=p){flag++;count+=(p-y[0]);}
if(y[num-1]>p){flag++;count+=(y[num-1]-p);}
if(flag==2)count+=aa;
}
}
printf("%d\n",count);
}
codeforces 486C Palindrome Transformation 贪心求构造回文的更多相关文章
- Codeforces 486C Palindrome Transformation(贪心)
题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...
- CodeForces 486C Palindrome Transformation 贪心+抽象问题本质
题目:戳我 题意:给定长度为n的字符串,给定初始光标位置p,支持4种操作,left,right移动光标指向,up,down,改变当前光标指向的字符,输出最少的操作使得字符串为回文. 分析:只关注字符串 ...
- Codeforces Round 486C - Palindrome Transformation 贪心
C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...
- codeforces 486C. Palindrome Transformation 解题报告
题目链接:http://codeforces.com/problemset/problem/486/C 题目意思:给出一个含有 n 个小写字母的字符串 s 和指针初始化的位置(指向s的某个字符).可以 ...
- USACO Prime Palindromes 构造回文数
这道题目一点也不卡素数的判断 就是朴素的sqrt(n) 也不卡 所以~放心的用吧. 构造回文的时候看了HINT 其中是这么写的: Generate palindromes by combining d ...
- python练习 之 实践出真知 中心扩展法求最大回文子串 (leetcode题目)
1 问题,给定一个字符串,求字符串中包含的最大回文子串,要求O复杂度小于n的平方. 首先需要解决奇数偶数的问题,办法是:插入’#‘,aba变成#a#b#a#,变成奇数个,aa变成#a#a#,变成奇数个 ...
- Codeforces Round #277 (Div. 2)C.Palindrome Transformation 贪心
C. Palindrome Transformation Nam is playing with a string on his computer. The string consists o ...
- codeforces 897B Chtholly's request 偶数长度回文数
codeforces 897B Chtholly's request 题目链接: http://codeforces.com/problemset/problem/897/B 思路: 暴力求出这100 ...
- hdu3613 Best Reward 扩展kmp or O(n)求最大回文子串
/** 题目:hdu3613 Best Reward 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意:有一个字符串,把他切成两部分. 如果这部 ...
随机推荐
- CentOS下安装netcat
CentOS下安装netcat 使用zookeeper过程中,需要监控集群状态.在使用四字命令时(echo conf | nc localhost 2181),报出如下错误:-bash: netcat ...
- BZOJ 1452:[JSOI2009]Count(二维树状数组)
[JSOI2009]Count 描述 输入 输出 1 2 分析: 裸二维bit,对每个颜色建一颗bit. program count; var bit:..,..,..]of longint; a:. ...
- 【POJ2774】Long Long Message (SA)
最长公共子串...两个字符串连在一起,中间放一个特殊字符隔开.求出height之后,枚举height,看两个后缀是不是分布于两段字符串..如果是,这个值就可以作为答案.取最大值即可. ; var c, ...
- shell if 条件语句实践
对于if 语法 我们不过多做介绍,这里直接上实例,以开发rsync服务启动脚本为例,先对rsync做个简单介绍 [root@backup ~]# rpm -qa|grep rsync rsync--. ...
- 初识laytpl
laytpl-精致巧妙的JavaScript模板引擎 这两天在做一个mui项目,列表需要循环很多的数据.在公司同事的指引下认识了这个新的模板--laytpl.我只想说,很好用们很巧妙. 废话不多说,直 ...
- 所驼门王的宝藏(bzoj 1924)
Description Input 第一行给出三个正整数 N, R, C. 以下 N 行,每行给出一扇传送门的信息,包含三个正整数xi, yi, Ti,表示该传送门设在位于第 xi行第yi列的藏宝宫室 ...
- GC+JVM
1.内存管理模型 ①以对象的方式管理内存,每个对象占据内存中连续的一段,分配在堆中.对象引用可以指向堆中的其他对象.非基本数据类型的对象等价于数据引用. ②基于栈和堆的内存管理都是动态分配,即在运行时 ...
- HTTP Header中Accept-Encoding
HTTP Header中Accept-Encoding 是浏览器发给服务器,声明浏览器支持的编码类型[1] 常见的有 Accept-Encoding: compress, gzip //支持comp ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---5
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: <Linux命令行与shell脚本 ...
- OpenMP参考链接
做个笔记. http://www.cnblogs.com/China3S/p/3500507.html