题目链接:Codeforces 486C Palindrome Transformation

题目大意:给定一个字符串,长度N。指针位置P,问说最少花多少步将字符串变成回文串。

解题思路:事实上仅仅要是对称位置不同样的。那么指针肯定要先移动到这里,改动字符仅仅须要考虑两种方向哪种更优即

可。

然后将全部须要到达的位置跳出来。贪心处理。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm> using namespace std; const int maxn = 1e5 + 5; int N, P;
vector<int> pos;
char s[maxn]; int solve () {
int ret = 0, n = N / 2;;
for (int i = 0; i < n; i++) {
int tmp = abs(s[i] - s[N-i-1]);
tmp = min(tmp, 26 - tmp);
ret += tmp;
if (tmp)
pos.push_back(abs(i+1-P) < abs(N-i-P) ? i+1 : N-i);
}
n = pos.size(); if (n == 0)
return ret;
sort(pos.begin(), pos.end());
return ret + pos[n-1] - pos[0] + min(abs(pos[n-1]-P), abs(pos[0]-P));
} int main () {
scanf("%d%d%s", &N, &P, s);
printf("%d\n", solve());
return 0;
}

Codeforces 486C Palindrome Transformation(贪心)的更多相关文章

  1. codeforces 486C Palindrome Transformation 贪心求构造回文

    点击打开链接 C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes ...

  2. CodeForces 486C Palindrome Transformation 贪心+抽象问题本质

    题目:戳我 题意:给定长度为n的字符串,给定初始光标位置p,支持4种操作,left,right移动光标指向,up,down,改变当前光标指向的字符,输出最少的操作使得字符串为回文. 分析:只关注字符串 ...

  3. Codeforces Round 486C - Palindrome Transformation 贪心

    C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...

  4. codeforces 486C. Palindrome Transformation 解题报告

    题目链接:http://codeforces.com/problemset/problem/486/C 题目意思:给出一个含有 n 个小写字母的字符串 s 和指针初始化的位置(指向s的某个字符).可以 ...

  5. Codeforces Round #277 (Div. 2)C.Palindrome Transformation 贪心

    C. Palindrome Transformation     Nam is playing with a string on his computer. The string consists o ...

  6. 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation

    题目传送门 /* 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 */ /************************************************ ...

  7. Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)

    Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input sta ...

  8. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  9. cf486C Palindrome Transformation

    C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input  ...

随机推荐

  1. canvas 下载

    function saveFile(data, filename){ var save_link = document.createElementNS('http://www.w3.org/1999/ ...

  2. 秒味课堂Angular js笔记------Angular js中的工具方法

    Angular js中的工具方法 angular.isArray angular.isDate angular.isDefined angular.isUndefined angular.isFunc ...

  3. Centos 5.2安装配置DNS服务器

    BIND安装配置(主从)我的系统环境:centos 5.2 作者:哈密瓜 主:我采用的是yum安装[root@linux src]#yum -y install bind* 生成rndc控制命令的ke ...

  4. 用CALayer实现淡入淡出的切换图片效果

    由于直接更改layer的contents显示的隐式动画切换的时候的动画持续时间没办法控制, 切换效果不尽人意,所以这里配合了CABasicAnimation实现淡入淡出的切换效果, 另外还可以使用组合 ...

  5. python的exec、eval详解

    exec exec语句用来执行储存在字符串或文件中的Python语句.例如,我们可以在运行时生成一个包含Python代码的字符串,然后使用exec语句执行这些语句.下面是一个简单的例子. exec ' ...

  6. The Primo ScholarRank Technology: Bringing the Most Relevant Results to the Top of the List

    By Tamar Sadeh, Director of Marketing In today’s world, users’ expectations for a quick and easy sea ...

  7. 2016最新一元云购完整源码 云购CMS系统 带安卓和ios手机客户端 源码免费分享

    原文转自:http://www.zccode.com/thread-724-1-1.html 该资源说明: 看起来不错的一套一元云购CMS源码,源码包里面带了安卓和ios手机客户端,手机客户端需要自己 ...

  8. php微信简单接口

    <?php $appid = "xxxxxxxxxx"; $secret = "xxxxxxxxxxxxxxxxxxxxxxxxx"; $url = &q ...

  9. memcache配置与使用

    php100:73:MemCached高级缓存配置 Memcache相关介绍: memcache 是一个高性能的分布式的内存对象缓存系统,它能够存储各种各样的的数据,包括图片,视频,文件等等.缓存功能 ...

  10. CentOS 添加/绑定 IP

    美国VPS的独立IP相对于国内而言,是非常的便宜的.比如有些美国VPS,买5个独立IP才三美元左右一个月.当我们购买了多个独立IP时,如果你不想再联系客服而漫长的等待,那就自己手动配置吧. 一.进入/ ...