题目描述
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.
这个题怎么说呢,就是求字符串的所有及字符串都是回文串,而奇回文串最终有2两种方式,一种是aaaaaaaa,另一种是ababababab这种情况,别的不可能,所以就可以将字符串遍历一遍,分为奇偶字符串,分别求出奇字符串中出现最多的字母,偶字符串也一样,最后只要用字符串的长度减去奇,偶字符串中出现字符的最大值之和就行了
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 105;
int main() {
int t,x,max1,max2;
int c[30],c2[30];
cin>>t;
char s[105];
while(t--)
{
memset(c,0,sizeof(c));
memset(c2,0,sizeof(c2));
max1 = max2 = -1;
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] > max1)
max1 = 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];
}
cout<<strlen(s) - max1 - max2<<endl;
}
return 0;
}