0

0

Codeforces Round #277 (Div. 2)-C. Palindrome Transformation (贪心)_html/css_WEB-ITnose

php中文网

php中文网

发布时间:2016-06-24 11:53:47

|

1265人浏览过

|

来源于php中文网

原创

Palindrome Transformation

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

立即学习前端免费学习笔记(深入)”;

Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.

There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1?≤?i?≤?n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i?-?1 if i?>?1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i?=?n, the cursor appears at the beginning of the string).

When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.

Initially, the text cursor is at position p.

Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?

Input

The first line contains two space-separated integers n (1?≤?n?≤?105) and p (1?≤?p?≤?n), the length of Nam's string and the initial position of the text cursor.

The next line contains n lowercase characters of Nam's string.

Output

Print the minimum number of presses needed to change string into a palindrome.

Sample test(s)

input

8 3aeabcaez

output

Note

A string is a palindrome if it reads the same forward or reversed.

In the sample test, initial Nam's string is:  (cursor position is shown bold).

In optimal solution, Nam may do 6 following steps:

The result, , is now a palindrome.

ColorMagic
ColorMagic

AI调色板生成工具

下载




解题思路:贪心。只考虑一边的情况即可,以左半为例,不断贪心考虑pos离哪边的不匹配边界近,如此往复即可。





AC代码:

#include <iostream>#include <cstdio>#include <string>#include <cmath>using namespace std;int main(){    int n, pos;    string s;//    freopen("in.txt", "r", stdin);    while(scanf("%d%d", &n, &pos)!=EOF){        pos --;        cin>>s;        int l = 0, r = n/2 - 1;        if(n/2 <= pos)  pos = n - 1 - pos;        while(l < n/2 && s[l] == s[n - 1 - l])  l++;      //找左边不匹配边界        while(r >= 0 && s[r] == s[n - 1 - r])  r--;       //<span style="font-family: Arial, Helvetica, sans-serif;">找左边不匹配边界</span>        int ans = 0;        for(int i=l; i<=r; i++){            int temp = abs(s[i] - s[n-1-i]);            ans += min(temp, 26 - temp);        }        if(l < r)            ans += min(abs(pos - l), abs(r - pos)) + r - l;        else if(l == r)            ans += abs(pos - r);        cout<<ans<<endl;    }    return 0;}





HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
漫蛙app官网链接入口
漫蛙app官网链接入口

漫蛙App官网提供多条稳定入口,包括 https://manwa.me、https

41

2026.02.27

deepseek在线提问
deepseek在线提问

本合集汇总了DeepSeek在线提问技巧与免登录使用入口,助你快速上手AI对话、写作、分析等功能。阅读专题下面的文章了解更多详细内容。

2

2026.02.27

AO3官网直接进入
AO3官网直接进入

AO3官网最新入口合集,汇总2026年可用官方及镜像链接,助你快速稳定访问Archive of Our Own平台。阅读专题下面的文章了解更多详细内容。

28

2026.02.27

php框架基础教程
php框架基础教程

本合集涵盖2026年最新PHP框架入门知识与基础教程,适合初学者快速掌握主流框架核心概念与使用方法。阅读专题下面的文章了解更多详细内容。

1

2026.02.27

php框架怎么用
php框架怎么用

本合集专为零基础学习者打造,系统介绍主流PHP框架的安装、配置与基础用法,助你快速入门Web开发。阅读专题下面的文章了解更多详细内容。

2

2026.02.27

无禁词AI聊天软件下载大全
无禁词AI聊天软件下载大全

本合集精选多款免费、无违禁词限制的AI聊天软件,支持自定义角色、剧情畅聊,体验真实互动感。阅读专题下面的文章了解更多详细内容。

6

2026.02.27

ai志愿助手2026
ai志愿助手2026

本合集汇总了2026年主流AI志愿助手官方入口及官网地址,涵盖圆梦志愿、蝶变志愿等智能填报平台,助你高效精准填志愿。阅读专题下面的文章了解更多详细内容。

1

2026.02.27

高清视频免费观看软件
高清视频免费观看软件

精选多款高清视频免费观看软件,涵盖海量电视剧、电影、综艺等资源,支持在线播放、无广告干扰、画质清晰流畅。阅读专题下面的文章了解更多详细内容。

8

2026.02.27

快看漫画地址大全
快看漫画地址大全

2026年快看漫画官方入口、APP下载地址及网页版在线阅读地址汇总,涵盖平台最新功能与热门作品推荐。阅读专题下面的文章了解更多详细内容

1

2026.02.27

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号