Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】
C. Palindrome Again !!
Given string with N characters, your task is to transform it to a palindrome string. It's not as easy as you may think because there is a cost for this transformation!!
First you have to start from character at given position P. From your position you always have 2 options:
- You can move one step to the right or to the left, the cost of each movement is 1. Assume that the string is cyclic, this means if you move one step to the left you will be at position P-1 if P > 1 or at the last character if P = 1, and if you move one step to the right you will be at position P+1 if P < N or at first character if P = N.
- You can change the letter at your current position by replacing it with the next or previous one in the English alphabet (assume that the alphabet is also cyclic so ‘a’ is after ‘z’). The cost of each replacement is also 1.
You should repeat that until the transformation is finished and the string is palindrome. What is the minimum cost to do that?
The first line contains the number of test cases T ( 1 ≤ T ≤ 100 ). Each test case contains 2 lines, the first line contains two integers ( 1 ≤ N ≤ 100,000) the length of string and ( 1 ≤ P ≤ N ) the initial position. While the second line contains a string with exactly N alphabetical characters.
For each test case output one line contains the minimum cost that is needed to change the string into a palindrome one.
1
8 3
aeabdaey
8
start with P = 3 ae(a)bdaey, move right => aea(b)daey, change to next => aea(c)daey, change to next => aea(d)deay, move left => ae(a)ddeay, move left => a(e)addeay, move left => (a)eaddeay, change to previous => (z)eaddeay, change to previous => (y)eaddeay. This costs 8 (4 movements and 4 replacements)
题目链接:http://codeforces.com/gym/100952/problem/C
题目大意:给出字符串a,将该字符串变成回文串!
分析:
字符串向左边或右边移动一步(0往前移一格为n-1看成环),花费为1
当前字母变为相邻字母,例如a -> b 或 a -> z, 花费为1
模拟此过程操作即可,代码给出了详细注释,一看就懂了!
下面给出AC代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 inline int read() 4 { 5 int x=0,f=1; 6 char ch=getchar(); 7 while(ch<'0'||ch>'9') 8 { 9 if(ch=='-') 10 f=-1; 11 ch=getchar(); 12 } 13 while(ch>='0'&&ch<='9') 14 { 15 x=x*10+ch-'0'; 16 ch=getchar(); 17 } 18 return x*f; 19 } 20 inline void write(int x) 21 { 22 if(x<0) 23 { 24 putchar('-'); 25 x=-x; 26 } 27 if(x>9) 28 write(x/10); 29 putchar(x%10+'0'); 30 } 31 int main() 32 { 33 int t; 34 t=read(); 35 while(t--) 36 { 37 int len,p; 38 len=read(); 39 p=read(); 40 p--;//从零下标开始计数 41 string s; 42 cin>>s; 43 //移动距离 44 int minn=1e+8; 45 int maxn=-1e+8; 46 int ans=0,flag=0; 47 for(int i=0,j=len-1;i<len/2;i++,j--) 48 { 49 if(s[i]!=s[j]) 50 { 51 int a=min(s[i],s[j])-'a'; 52 int b=max(s[i],s[j])-'a'; 53 ans+=min(b-a,a+26-b);//变换需要移动的最短距离 54 minn=min(minn,i);//满足条件最近的下标值 55 maxn=max(maxn,i);//满足条件最远的下标值 56 flag=1; 57 } 58 } 59 if(!flag)//如果序列已经是回文序列 60 { 61 printf("0\n"); 62 continue; 63 } 64 if(len%2==1&&p==len/2)//奇数长度的回文序列,查找下标刚好是其中点时 65 { 66 ans+=abs(p-minn);//移动距离为其长度的一半 67 printf("%d\n",ans); 68 continue; 69 } 70 if(p>=len/2) 71 p=len-p-1;//这是一个回文序列,是对称的,所以我们采取序列号从小往大的排列方式 72 int c=abs(minn-p); 73 int d=abs(maxn-p); 74 ans+=min(c,d);//在变换过程中会忽略那些对称的点,所以这步是要加上那些忽略点的距离 75 ans+=(maxn-minn);//起始变换点与变换终点的距离,也就是移动距离 76 printf("%d\n",ans); 77 } 78 return 0; 79 }
作 者:Angel_Kitty
出 处:https://www.cnblogs.com/ECJTUACM-873284962/
关于作者:阿里云ACE,目前主要研究方向是Web安全漏洞以及反序列化。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
欢迎大家关注我的微信公众号IT老实人(IThonest),如果您觉得文章对您有很大的帮助,您可以考虑赏博主一杯咖啡以资鼓励,您的肯定将是我最大的动力。thx.

我的公众号是IT老实人(IThonest),一个有故事的公众号,欢迎大家来这里讨论,共同进步,不断学习才能不断进步。扫下面的二维码或者收藏下面的二维码关注吧(长按下面的二维码图片、并选择识别图中的二维码),个人QQ和微信的二维码也已给出,扫描下面👇的二维码一起来讨论吧!!!

欢迎大家关注我的Github,一些文章的备份和平常做的一些项目会存放在这里。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述