Mike and palindrome

麦克有一个只包含小写字母的字符串 s 。他想知道他能否恰好改变这个字符串的一个字母使得这个字符串成为回文串。

Example

Input

abccaa

Output

YES

Input

abbcca

Output

NO

Input

abcda

Output

YES

容易遗漏的一种情况是'ABCBA' ,其他的没有什么问题。

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<iomanip>
#include<queue>

typedef long long ll;
const int inf = 0x3f3f3f3f;

using namespace std;

#define maxsize 600

char s[20];
char s1[20];
int top,mid,ans;

int main(){
    
    while(cin >> s)
    {
        int i = 0;
        int len = strlen(s);
        top = 0;

        if(len == 1) printf("YES\n");

        else
        {
            mid = len / 2;
            for(i = 0;i < mid;i++)
            {
                s1[top++] = s[i];
            }
            if(len % 2 == 0)
            {
                for(i = mid;i < len;i++)
                {
                    if(s1[--top] != s[i]) ans++;
                }
            }

            else
            {
                for(i = mid + 1;i < len;i++)
                {
                    if(s1[--top] != s[i]) ans++;
                }
                if(!ans) ans++;        //遇到ABCBA这种情况
            }

            if(ans == 1) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值