备战省赛--Super-palindrome

版权声明:转载请附上原文链接哟! https://blog.csdn.net/weixin_44170305/article/details/89742578

如花美眷,似水流年,回得了过去,回不了当初。

题目描述

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

提示

For second test case aaaaba, just change letter b to a in one step.

题目大意:就是给你一个字符串,让你改为超级回文字符串(如果字符串具有奇数长度的所有子串都是回文串,则该字符串称为超回文 字符串。也就是说,对于字符串s,如果其子串s i ... j满足j-i + 1是奇数,那么对于  k = 0,1,...,j-i + 1,s i + k = s j-k),您可以将每个字符串中的一个字符更改为另一个 字母。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char s[110];
int main()
{
    int T;
    int i;
    int len;
    int cnt1,cnt2;
    int as;
    int b[30],c[30];
    int m[30],n[30];
    scanf("%d",&T);
    while(T--)
    {
        memset(b,0,sizeof(b));
        memset(m,0,sizeof(m));
        scanf("%s",s);
        len=strlen(s);
        cnt1=0,cnt2=0;
        for(i=0; i<len; i+=2)
        {
            as=s[i];
            b[as-97]++;
            if(b[as-97]==1)
                c[cnt1++]=as-97;
        }
        for(i=1; i<len; i+=2)
        {
            as=s[i];
            m[as-97]++;
            if(m[as-97]==1)
                n[cnt2++]=as-97;
        }
        int max1=0,max2=0;
        for(i=0; i<cnt1; i++)
        {
            if(max1<b[c[i]])
                max1=b[c[i]];
        }
        for(i=0; i<cnt2; i++)
        {
            if(max2<m[n[i]])
                max2=m[n[i]];
        }
        printf("%d\n",len-max1-max2);
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/weixin_44170305/article/details/89742578