Super-palindrome HDU - 6264 (改变字符,把每个字符串的奇数子串都变成回文的,一定要善于观察)

本文介绍了一种算法挑战,即如何通过最小步数将任意字符串转换为超级回文串,其中所有奇数长度的子串都必须是回文串。文章详细解释了题目要求、输入输出格式及核心思路,并提供了C语言实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述
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;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值