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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值