Problem A. 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.
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,2...j-i+1.
Input
The first line contains an integerT(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 isguaranteed that the length of string
satisfies1<=|s|<=100.
Output
For each test case, print one line with an integer refers to the minimum steps to take.
Example
standard input
3
ncncn
aaaaba
aaaabb
standard output
0
1
2
Explanation
For second test case aaaaba, just change letter b to a in one step.
解题思路:这题很简单,列举的时候就会发现这个只有2种情况,要么aaaaa类型,要么ababababab这种类型的。
然后将位置分奇偶,各统计每个字母的出现的数字,然后找出这奇数偶数位置的最大值,然后用长度减去这2个最大值
就是走的最少的step了。
#include<bits/stdc++.h>
using namespace std;
int a[28],c[28],d[101],e[101];
char b[150];
int main(void)
{
int T;
scanf("%d",&T);
while(T--)
{
memset(a,0,sizeof a);
memset(c,0,sizeof c);
int k=0,p=0,len1,len2;
scanf("%s",b);
int len=strlen(b);
for(int i=0;i<len;i++)
{
if(i%2)
a[b[i]-'a']++;
else
c[b[i]-'a']++;
}
if(len%2)
{
len1=len/2;
len2=len/2+1;
}
else
{
len1=len/2;
len2=len/2;
}
for(int i=0;i<26;i++)
{
if(a[i]!=0)
d[k++]=a[i];
if(c[i]!=0)
e[p++]=c[i];
}
//for(int i=0;i<k;i++) cout<<d[i]<<" "; cout<<endl;
//for(int i=0;i<p;i++) cout<<e[i]<<" "; cout<<endl;
sort(d,d+k);
sort(e,e+p);
int ab=d[k-1];
int cd=e[p-1];
int ans1=len1-ab;
int ans2=len2-cd;
printf("%d\n",ans1+ans2);
}
return 0;
}
.