<span>Mike and palindrome CodeForces - 798A</span>

题目链接

一个简单的题目,但是却很少有人可以一次AC,比如我就瞎写wa了一次。。。

写本博算个教训录吧。

题目给出一个字符串,让你严格的改变一个字符使改变后的字符串是一个回文串。

回文串不用解释了。不懂自行百度。

需要注意两点:

1.如果长度为偶数,并且事先就是一个回文串,那么要输出no的,因为必须要改变一个字符串,在原本就是回文的基础上改变一下就不是回文串了。

2.如果长度为奇数,并且事先就是一个回文串,那么要输出yes,因为可以只改变最中间的那个字符,改后还是一个回文串。

其他的就是判断有几对字符不一样了,是一对的话就yes,不是就no。

我的AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=1000010;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[maxn];

int main()
{
    scanf("%s",s);
    int len=strlen(s);
    int l=0;
    int cnt=0;
    int r=len-1;
    while(l<=r)
    {
        if(s[l]!=s[r])
        {
            cnt++;
        }
        l++;
        r--;

    }
    if(cnt==1||(cnt==0&&(len%2==1)))
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}
View Code

 

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务