HDU - 6264 - Super-palindrome(思维)

题意:

给出一个字符串,使得所有的奇数连续子串为回文串,输出最小变化次数

思路:

分析过后,只存在两种情况,1全部为一种字母,2形如abab交替类型

对于奇数位和偶数位单独计数,只需计算出奇数位或者偶数位出现最多字母的次数

情况12其实是一样的解法,情况1可以看为2的一种特殊情况,只需要考虑情况2
找出出现最多的字母的次数,把他作为基底,全部替换为这个字母即可

字符串长度 - 奇数字母次数 - 偶数字母次数 即可

代码:

#include<iostream>
#include<cstring>
#include<map>
#include<algorithm>
#define max(a, b) (a)>(b)?(a):(b)
#define min(a, b) (a)>(b)?(b):(a)
using namespace std;
map<char, int> a,b;
int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        int aa = 0, bb = 0, ans = 99999;
        char str[110];
        a.clear();
        b.clear();
        scanf("%s", str);
        int len = strlen(str);
        for(int i = 0; i < len; i++) {
            if((i+1)%2) {
                a[str[i]]++;
                if(aa < a[str[i]]) aa = a[str[i]];
            } 
            else {
                b[str[i]]++;
                if(bb < b[str[i]]) bb = b[str[i]];
            }
        }
        printf("%d\n", len - aa - bb); 
    }
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/somliy/p/9726692.html