2018年省赛热身赛第9场-A - 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 substrings S(i...j )satisfies j-i+1 is odd then Si+k)=S(j-k) for k = 0,1,...,j-i+1.

输入

The first line contains integer T(1≤T≤100) representing the number of test case.

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 intger refers to the minimum steps to take.

 题意:

一个字符串s,作出最少的改动,使它的子串中长度为奇数的子串是回文的。

举个例子:

有一串字符串(用数字表示字母)1  2 3 4 5 6 7 8

1.如果要满足长度为奇数的子串都是回文的,比如1 2 3 要是回文的话, 1 、3代表的字符必须相等,以此类推1、3、5、7必须相等,同理, 2 3 4要是回文的话,2、4代表的字符必须相等,以此类推2、4、6、8必须相等,

2.那么就是统计一下每个奇数位个字符的出现次数,找到奇数位出现次数最大的那个字符,记它出现的次数为x,偶数位的那个为 y,

3.最小改变次数 = 字符串的长度 - x - y.

这题还是比较好理解的,这次训练赛就勉强在同学帮助下做出这题...

#include <iostream>

int main() {
    int t,x,max,max2;
    int c[30],c2[30];
    std::cin>>t;
    char s[105];
    while(t--)
    {
        memset(c,0,sizeof(c));
        memset(c2,0,sizeof(c2));
        max =  max2 = -1;
        std::cin>>s;

        for(int i =0 ; i < strlen(s);i += 2)
        {
            x = s[i] - 'a' ;
            c[x] ++;
        }
        for(int i =0; i < 30;i++)
        {
            if(c[i] > max)
                max = c[i];
        }


        for(int i =1 ; i < strlen(s);i += 2)
        {
            x = s[i] - 'a' ;
            c2[x] ++;
        }
        for(int i =0; i < 30;i++)
        {
            if(c2[i] > max2)
                max2 = c2[i];
        }
        std::cout<<strlen(s) - max - max2<<std::endl;

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/81982927