题目描述
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;
}
}