题目:戳我

  题意:给定长度为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. MySql每月增加一个分区以及查询所有分区

    create PROCEDURE Usp_Partition() BEGIN DECLARE _time datetime; DECLARE num int; DECLARE _p VARCHAR(2 ...

  2. Eclipse4.3正式版已发布

    Eclipse4.3正式版已发布,传送门http://www.eclipse.org/downloads/

  3. PHP CI框架最近学到的内容

    CI框架配置方面注意的细节 在config里面的database.php里面是和数据库配置相关的内容 $db['default'] = array( 'dsn' => '', 'hostname ...

  4. bzoj 2763: [JLOI2011]飞行路线 分层图

    题目链接 n个点m条路, 每条路有权值,  给出起点和终点, 求一条路使得权值最小.可以使路过的路中, k条路的权值忽略. 其实就是多一维, 具体看代码 #include<bits/stdc++ ...

  5. python实现进度条

    先说一下文本系统的控制符: \r: 将光标移动到当前行的首位而不换行: \n: 将光标移动到下一行,并不移动到首位: \r\n: 将光标移动到下一行首位. 环境: root@ubuntu16:/ale ...

  6. struts2上传下载

    struts上传下载必须引入两个jar文件: commons-fileupload-x.x.x.jar和comons-io-x.x.x.jar上传文件 import java.io.BufferedI ...

  7. centos7 install magento

    http://mirrors.yun-idc.com/epel/7/x86_64/e/epel-release-7-2.noarch.rpmrpm --import /etc/pki/rpm-gpg/ ...

  8. android studio recent projects

    android studio正常的删除项目的方法是可以点击右键,选择project structure,进入到界面时,选择你要删除的工程,点击减号,接着就可以右键工程有一个delete. 另外一种方法 ...

  9. hdu 4119 Isabella's Message

    Isabella's Message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  10. pre标签 首行会自动换行解决方案

    利用pre标签可以 解决文本文档里面的空格及换行在页面上不显示的方案, 自行换行 加 white-space: pre-wrap; word-wrap: break-word; 英文字母换行 word ...