题目:戳我

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

  分析:只关注字符串n/2长度,up,down操作是固定不变的,也就是不能优化了,剩下就是left,down的操作数,细想下中间不用管,只关注从左到中间第一个要改变的位置和最后一个要改变的位置即可,具体看代码。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int M = 1e5+; int n, p;
char str[M];
int main() {
while( ~scanf("%d %d", &n, &p ) ) {
getchar();
gets( str+ );
int sumchg = ; //up,down的操作总数
int first = ; //第一个要改变的位置
bool firstjd = true;
int last = ; //最后一个要改变的位置
for( int i=; i<=n/; i++ ) {
int d = abs( str[i]-str[n+-i] );
if( d ) {
if( firstjd ) {
first = i;
firstjd = false;
}
last = i;
sumchg += min( d, -d ); //选择up或者down的最小操作数
}
}
if( p > n/ ) //由于回文左右对称,所以p在中间右边时也可将p当左边对称位置计算
p = n+-p;
int ret = ;
if( sumchg == ) { //不需要改变输出0
printf("%d\n", ret);
continue;
}
if( first >= p ) //如果p在第一个要改变的左边,p只能向右走,即执行right操作
ret += sumchg + last - p;
else if( last <= p ) //如果p在最后一个要改变的右边,p只能向左走,即执行left操作
ret += sumchg + p - first;
else
ret += min( *(p-first)+last-p, *(last-p)+p-first ) + sumchg; //p在中间,取求向左向右走的最小值
printf("%d\n", ret);
}
return ;
}

CodeForces 486C Palindrome Transformation 贪心+抽象问题本质的更多相关文章

  1. Codeforces 486C Palindrome Transformation(贪心)

    题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...

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

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

  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. Hadoop学习笔记(3)hadoop伪分布模式安装

    为了学习这部分的功能,我们这里的linux都是使用root用户登录的.所以每个命令的前面都有一个#符号. 伪分布模式安装步骤: 关闭防火墙 修改ip地址 修改hostname 设置ssh自动登录 安装 ...

  2. Yii2.0中文开发向导——rules常用规则

    public function rules(){ return [ //必须填写 ['email, username, password,agree,verifyPassword,verifyCode ...

  3. 打印 PHP $_SERVER 常量

    foreach( $_SERVER as $var => $value){ echo $var.' '.$value.'<br>'; };

  4. [LeetCode]题解(python):068-Text Justification

    题目来源: https://leetcode.com/problems/text-justification/ 题意分析: 输入一个字符串数组和一个规定长度L.将这个字符串数组的元素尽可能放到长度的L ...

  5. web附件中文名

    response.setHeader("Content-Disposition", "attachement;filename="+URLEncoder.enc ...

  6. Android GPS应用:动态获取位置信息

    在上文中,介绍了GPS概念及Android开发GPS应用涉及到的常用类和方法.在本文中,开发一个小应用,实时获取定位信息,包括用户所在的纬度.经度.高度.方向.移动速度等.代码如下: Activity ...

  7. c 判断水仙花数,质数(素数)

    #include<stdio.h> #include<stdbool.h> //水仙花数--各位立方和等于本身 void sXh() { int x,y,z; printf(& ...

  8. 使用超链接跳转页面(GridView)

    1. the html markup <div> <asp:GridView ID=" OnPageIndexChanging="GridView1_PageIn ...

  9. 远程唤醒、WOL、Magic_Packet

    背景:很多人熟悉远程桌面并经常地利用它所带来的方便,但是前提是服务器(远程电脑)必须是处于开机状态.对于机房里有专人管理的服务器,这点不成问题,但如果是放在家里的电脑,要让它7*24地开机似乎就不好办 ...

  10. [译]Stairway to Integration Services Level 13 - SSIS 变量回顾

    介绍 在前一篇中我们组合了已经学过的事件冒泡 event bubbling, 日志记录 logging, 和父子模型 Parent-Child pattern 建立了自定义的SSIS包日志记录. 本文 ...