寒假训练,2.25,J-Palindrome Names (回文

本文介绍了一种算法,用于计算将任意名字最少修改几次可变为回文串的方法。通过对比字符并计数非匹配项,该算法能高效确定所需更改次数。

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

题目:

Anna and Bob are having a baby. They both enjoy the advantage of having palindrome names, meaning that their names are spelled the same way forwards and backwards. Wanting to be good parents, they decide to give their child a palindrome name too. The only problem is that they aren’t sure if the one they picked is a palindrome. If it turns out it isn’t a palindrome, they want to change it to a palindrome using as few changes as possible. The allowed changes are:
  • Change one letter of the name.
  • Add a letter to the end of the name.
Help Bob and Anna find out how many changes they need to make to the name to make it a palindrome.

 

Input

Input is the name they have chosen.

 

Output

Output the number of changes they need to make.

 

 

Limits

  • The length of the name is at least 11 and at most 100100 characters.
  • The name consists of only lowercase letters a–z.

 

 

 

#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
char str[102];
int num[102];
int cntt(int i,int j){
    int cnt=0;
    while(i<j){
        if(str[i]!=str[j-1]){cnt++;}
        i++;j--;
    }
    return cnt;
}
int main(){
    while(~scanf("%s",str)){
        int minn;
        int len=strlen(str);
        for(int i=0; i<len/2; i++){
            num[i]=i+cntt(i,len);
		}
        minn=num[0];
        for(int i=1;i<len/2;i++){
            minn=min(minn,num[i]);
        }
        printf("%d\n",minn);
    }
}

  我到现在没想通什么原理,但真的好方便啊

转载于:https://www.cnblogs.com/ineedyou/p/8471200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值