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
原创粉丝点击