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<string>
#include<cstring>
#include<algorithm>

using namespace std;

const int maxn = 26+2;
int odd[maxn];
int even[maxn];
int len;
int main()
{
    int t;
    cin>>t;
    while(t--){
        memset(odd,0,sizeof(odd));
        memset(even,0,sizeof(even));
        string s;
        cin>>s;
        len = s.length();
        for(int i = 0;i < len;i++){
            if(i%2 == 0)
                even[s[i]-'a']++;
            else
                odd[s[i]-'a']++;
        }
        sort(even,even+27,greater<int>() );
        sort(odd,odd+27,greater<int>() );
        cout<<len-even[0]-odd[0]<<endl;
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值