题目描述
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.
题意:输入一个t,表示有几组数据,下面输入一个长度不超过 100的字符串,让你改变其中最少的字符,使得只要是长度为奇数的子串都是回文字符串;
思路:这道题,你一定要善于观察,一定要抓住字眼,长度为奇数的子串都是回文字符串;
超级回文串:只要是长度为奇数的子串都是回文字符串;
都是先定义为从下标为1开始的子串
长度为 3 时 要想使回文序列的话 a[1] = a[3]; 长度增长
长度为5 时 要想回文序列 a[1]=a[5] a[2]=a[4],长度为3时,要想使回文 ,a[1]=a[3],那么现在,a[1]=a[5] =a[3] 长度增长
长度为7时 a[1] = a[7],a[2] = a[6],a[3] = a[5], 综合 长度为3和为5知,a[1] = a[3] =a[5] =a[7],a[2] = a[4] = a[6];
可以不是从下标为1的子串开始,可是只要的道理;
综上: 要是是超级回文串,所有的奇数为必须相同,所有的偶数时必须相同
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 110
char str[Max];
int main()
{
int i,j,k,t;
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
int l = strlen(str);
int a[30];
memset(a,0,sizeof(a));
for(i = 0;i<l;i+=2)
a[str[i]-'a']++;
int sum = 0;
int Ma = 0;
for(i =0;i<26;i++)
Ma = max(Ma,a[i]);
sum +=Ma;
memset(a,0,sizeof(a));
for(i = 1;i<l;i+=2)
a[str[i]-'a']++;
Ma = 0;
for(i=0;i<26;i++)
Ma = max(Ma,a[i]);
sum +=Ma;
printf("%d\n",l-sum);
}
return 0;
}