Super-palindrome

题目描述

You are given a string that is consisted of lowercase English alphabet. You are supposed to change it into a super-palindrome string in minimum steps. You can change one character in string to another letter per step.
A string is called a super-palindrome string if all its substrings with an odd length are palindrome strings. That is, for a string s, if its substring si…j satisfies j - i + 1 is odd then si+k = sj-k for k = 0,1,…,j-i+1.

输入

The fi rst line contains an integer T (1≤T≤100) representing the number of test cases.
For each test case, the only line contains a string, which consists of only lowercase letters. It is guaranteed that the length of string satisfies 1≤|s|≤100.

输出

For each test case, print one line with an integer refers to the minimum steps to take.

样例输入
3
ncncn
aaaaba
aaaabb

样例输出
0
1
2

思路
找出奇数位和偶数位的出现最频繁的字符的个数,将字符串的长度减去两个值,即为要替换的个数

代码实现

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
 
using namespace std;
const int N=105;
char s[N];
int T;
int even[N],odd[N];
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        int m1=0,m2=0;
        memset(even,0,sizeof(even));
        memset(odd,0,sizeof(odd));
        scanf("%s",s);
        int l=strlen(s);
        for(int i=0;i<l;i+=2)
        {
            even[s[i]-'a']++;
            if(even[s[i]-'a']>m1) m1=even[s[i]-'a'];
        }
        for(int i=1;i<l;i+=2)
        {
            odd[s[i]-'a']++;
            if(odd[s[i]-'a']>m2) m2=odd[s[i]-'a'];
        }
        printf("%d\n",l-m1-m2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43935894/article/details/89046517