2018年省赛热身赛第9场-Problem A. Super-palindrome

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;
}

 

.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值