seo网站推广技术,企业简介ppt范文大全,一个人看的免费视频高清直播,手机必备app排行榜题目#xff1a;戳我 题意#xff1a;给定长度为n的字符串#xff0c;给定初始光标位置p#xff0c;支持4种操作#xff0c;left#xff0c;right移动光标指向#xff0c;up#xff0c;down#xff0c;改变当前光标指向的字符#xff0c;输出最少的操作使得字符串为回… 题目戳我 题意给定长度为n的字符串给定初始光标位置p支持4种操作leftright移动光标指向updown改变当前光标指向的字符输出最少的操作使得字符串为回文。 分析只关注字符串n/2长度updown操作是固定不变的也就是不能优化了剩下就是leftdown的操作数细想下中间不用管只关注从左到中间第一个要改变的位置和最后一个要改变的位置即可具体看代码。 #include iostream
#include cstdio
#include cstring
#include algorithm
using namespace std;
const int M 1e55;int n, p;
char str[M];
int main() {while( ~scanf(%d %d, n, p ) ) {getchar();gets( str1 );int sumchg 0; //updown的操作总数int first 0; //第一个要改变的位置bool firstjd true;int last 0; //最后一个要改变的位置for( int i1; in/2; i ) {int d abs( str[i]-str[n1-i] );if( d ) {if( firstjd ) {first i;firstjd false;}last i;sumchg min( d, 26-d ); //选择up或者down的最小操作数}}if( p n/2 ) //由于回文左右对称所以p在中间右边时也可将p当左边对称位置计算p n1-p;int ret 0;if( sumchg 0 ) { //不需要改变输出0printf(%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;elseret min( 2*(p-first)last-p, 2*(last-p)p-first ) sumchg; //p在中间取求向左向右走的最小值printf(%d\n, ret);}return 0;
} 转载于:https://www.cnblogs.com/TaoTaoCome/p/4700216.html