2079 : su
per-palindrome
时间限制:0 Sec 内存限制:0 MiB
提交:32 答案正确:11
题目描述
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.
题意
给你一串小写字母组成的字符串。你需要用最少的步骤把这个字符串变成超级回文串。每个步骤你都可以把字符串里的一个字符变成其它字符。
超级回文串 :一个字符串它的所有长度为奇数的子串都是回文串。
思路:把一个字符串变成超级回文串,只需要让它奇数位置的字符都相同偶数位置的字符都相同。
比如 ncncncn
它的奇数位置的字符都相同,偶数位置的字符也都相同,所以它是超级回文串。
比如说aaaaabb
让它奇数位置的字符都相同最少要1步。
让它偶数位置的字符都相同也最少要1步。
那么把它变成超级回文串最少需要1+1 也就是2步。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
char str[110];
int ch[1000];
int main()
{
int T,cnt1,cnt2,cnt;
scanf("%d",&T);
while(T--)
{
memset(str,'\0',sizeof(str));
memset(ch,0,sizeof(str));
scanf("%s",str);
cnt1=cnt2=99999;
for(int i=0; i<strlen(str); i++)
{
cnt=0;
if(i%2==0)
{
int j=0;
for(; j<strlen(str); j+=2)
{
if(str[j]!=str[i])
{
cnt++;
}
}
cnt1=cnt1<cnt ? cnt1 :cnt;///找偶数位置要做的最少变化
}
else
{
int j=1;
for(; j<strlen(str); j+=2)
{
if(str[j]!=str[i])
{
cnt++;
}
}
cnt2=cnt2<cnt ? cnt2 :cnt;///找奇数位置要做的最少变化
}
}
printf("%d\n",cnt1+cnt2);
}
return 0;
}