题目描述
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.
题目中有写到从j到j+i正序逆序都是一样的,刚开始忽略了这个完全不对,题目中关于对数字的介绍一定要仔细看,样例太普通,你可能就会理解错题意
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
getchar();
char x[110];
while(n--)
{
int c=0;
int m=100;
gets(x);
for(int i=0;i<strlen(x);i++)
{
c=0;
for(int j=0;j<strlen(x);j++)
{
if(x[j]!=x[i])
{
c++;
}
}
if(c<m)
{
m=c;
}
}
int f=0;
char a[110];
for(int i=0;i<strlen(x);i++)
{
int flag=0;
for(int j=0;j<f;j++)
{
if(a[j]==x[i])
{
flag=1;
break;
}
}
if(flag==0)
{
a[f++]=x[i];
}
}
if(f>1)
{
for(int j=0;j<f-1;j++)
{
for(int k=j+1;k<f;k++)
{
c=0;
for(int w=0;w<strlen(x);w++)
{
if(w%2==0)
{
if(x[w]!=a[j])
{
c++;
}
}
else
{
if(x[w]!=a[k])
{
c++;
}
}
}
if(c<m)
{
m=c;
}
c=0;
for(int w=0;w<strlen(x);w++)
{
if(w%2==0)
{
if(x[w]!=a[k])
{
c++;
}
}
else
{
if(x[w]!=a[j])
{
c++;
}
}
}
if(c<m)
{
m=c;
}
}
}
}
printf("%d\n",m);
}
return 0;
}