Mike and palindrome CodeForces - 798A

时间:2023-03-08 23:40:35
Mike and palindrome CodeForces - 798A

题目链接

一个简单的题目,但是却很少有人可以一次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=;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[maxn]; int main()
{
scanf("%s",s);
int len=strlen(s);
int l=;
int cnt=;
int r=len-;
while(l<=r)
{
if(s[l]!=s[r])
{
cnt++;
}
l++;
r--; }
if(cnt==||(cnt==&&(len%==)))
printf("YES\n");
else
printf("NO\n");
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}