CodeForces 486C Palindrome Transformation(贪心)
来源:互联网 发布:常州巨人网络待遇 编辑:程序博客网 时间:2024/05/17 21:55
CodeForces 486C Palindrome Transformation(贪心)
CodeForces 486C
题目大意:
将一个不是回文的字符串通过最少的操作使之成为回文串。
操作,左移1位,右移1位,字母+1,字母-1,这些操作到达边界时是有循环的效果的,例如位置到达最后一位往右移动,那么就到达了第一位。
解题思路:
首先需要统计一下有多少个位置是不匹配的,并且将两个不一样的字母通过最少的操作转换成一样的。然后就是移动位置的计算。
一开始还在想是不是都往一个方向移动好呢,还是移动再返回好呢,或者是移动从后边再返回到前面好呢,后面发现其实只要每次选取最近的点就可以了。因为是回文串,位置是对称的。
代码:
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn = 1e5 + 5;char str[maxn];int N, P;vector<int> pos;int min (const int a, const int b) { return a < b ? a: b;}int is_palindrome() { int ans = 0; int count = 0; pos.clear(); for (int i = 0; i < N/2; i++) { if (str[i] != str[N - 1 - i]) { count++; int gap = abs(str[i] - str[N - 1 - i]);// printf ("%d\n", gap); ans += min(gap, 26 - gap); pos.push_back(abs(i - P) < abs(N - 1 - i - P) ? i + 1 : N - i); } } // printf ("%d%d\n", ans, count); sort(pos.begin(), pos.end());// printf ("%d %d\n", pos[count - 1], pos[0]); if (ans) ans += pos[count - 1] - pos[0] + min(abs(pos[0] - P), abs(pos[count - 1] - P)); return ans; }int main () { while (scanf ("%d%d", &N, &P) != EOF) { scanf ("%s", str); printf ("%d\n", is_palindrome()); } return 0;}
0 0
- Codeforces 486C Palindrome Transformation(贪心)
- codeforces 486C Palindrome Transformation(贪心)
- CodeForces 486C Palindrome Transformation(贪心)
- codeforces 486c-Palindrome Transformation【贪心】
- CodeForces 486C Palindrome Transformation 贪心
- Codeforces 486C Palindrome Transformation 贪心
- codeforces 486-C. Palindrome Transformation(贪心)
- codeforces 486C Palindrome Transformation 贪心求构造回文
- codeforces 486C Palindrome Transformation 贪心求构造回文
- codeforces 277 C. Palindrome Transformation (贪心)
- CodeForces 486C Palindrome Transformation
- Codeforces 486 C Palindrome Transformation
- codeForces 486C Palindrome Transformation
- Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)
- Palindrome Transformation - CodeForces 486 C 水题
- Codefroces 486C Palindrome Transformation【贪心】好题
- CodeForces 486C Palindrome Transformation(回文串)
- CF 486C Palindrome Transformation
- Mysql中事务隔离级别与binlog_format的一点理解
- word2007如何实现将正文字体全部改成其它的大小
- java高分局之JVM命令参数大全(标准选项)
- iOS UITextView以设置边距
- MFC编程,Cview类OnDraw重绘时,视图内容消失问题的解决
- CodeForces 486C Palindrome Transformation(贪心)
- Core Data概述
- WP8移植过程遇到的各种坑
- HTML学习笔记-v1.3
- [html][css]我的编码习惯
- 黑马程序员学习日记--IO流技术(二)
- ios开发:zxing 二维码
- JavaScript 的性能优化:加载和执行
- listView的工作原理