Mike and palindrome

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/KobeSacre/article/details/83896055

麦克有一个只包含小写字母的字符串 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;
}

猜你喜欢

转载自blog.csdn.net/KobeSacre/article/details/83896055
今日推荐