题目链接: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. Eclipse 运行ant build.xml

    在命令行cmd运行mvn clean install,ant compiler,提示上述信息,是因为 maven的这个插件要求jdk1.6,但是本地电脑环境变量jdk版本为1.7.将JAVA_HOME ...

  2. (转)Repeater在无数据记录时显示暂无数据

    方法就是在FooterTemplate加个Label并根据repeater.Items.Count判断是否有记录.关键代码如下: <FooterTemplate>     <asp: ...

  3. PL/SQL中的变量

    1.标量: ag1: declare v_ename emp.ename%type;--自己称为单变量 begin select ename into v_ename from emp where e ...

  4. 微信公众平台开发(一) ——实现URL接入

    一.填写服务器配置 登录微信公众平台,点击开发者中心,点击“修改配置”按钮,填写服务器地址(URL).Token和EncodingAESKey.URL是开发者用来接收微信消息和事件的接口URL.Tok ...

  5. UBUNTU 下设置全局 path变量

    全局的对所有用户都可以的使用的PATH: 可以通过修改配置文件: /etc/bashrc 和 /etc/profile 来时配置, 全局的PATH; 例如: vi /etc/profile 在最后后加 ...

  6. HAPROXY 配置项/配置实例

    HAPROXY 配置项/实例 常用配置选项: OPTION 选项: option httpclose :HAProxy会针对客户端的第一条请求的返回添加cookie并返回给客户端,客户端发送后续请求时 ...

  7. asp.net core VS goang web[修正篇]

    先前写过一篇文章:http://www.cnblogs.com/gengzhe/p/5557789.html,也是asp.net core和golang web的对比,热心的园友提出了几点问题,如下: ...

  8. javascript 版的 SuperMario

    注册博客园4年了,还没发表过一篇文章.明年就是超级马里奥这款游戏诞生30周年了,作为一个喜欢2d卷轴游戏的玩家,决定用js实现一个.目前只实现了基本玩法,得分什么的也还没做.项目的GitHub的地址是 ...

  9. 正在搞用web.py做的通讯录

    正在搞用web.py做的通讯录,应该算是学习吧,学习那个web.py的应用框架,其实已经搞起来了,可以增删改数据了,查询还没可以用,现在正在搞登录认证的问题,久没用python, 但再上手后半小时就有 ...

  10. 转:Java反射教程

    原文来自于:http://www.importnew.com/9078.html 什么是反射?反射有什么用处? 1. 什么是反射? “反射(Reflection)能够让运行于JVM中的程序检测和修改运 ...