(水题)题目链接:点击打开链接
Mike has a string s consisting of only lowercase English letters. He wants tochange exactly one character from the string so that the resulting one is a palindrome.
A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.
The first and single line contains string s (1 ≤ |s| ≤ 15).
Print "YES" (without quotes) if Mike can changeexactly one character so that the resulting string is palindrome or "NO" (without quotes) otherwise.
abccaa
YES
abbcca
NO
abcda
YES
题目大意:给定一个字符串,判断能否改且仅改变一个字母使之成为回文串。
分析:只需要对该字符串统计对称位置上的字母不相同的个数x,如果x==1,则YES;如果x!=1,看字符串的长度是否为奇数且x=0,如果成立则YES,否则NO!
下面是AC代码
#include<cstdio>
#include<cstring>
int main()
{
char s[20];
scanf("%s",s);
int len=strlen(s);
int cnt=0;
for(int i=0;i<len/2;i++)
{
if(s[i]!=s[len-1-i])
cnt++;
}
if(!cnt&&len%2) //这种情况第一次没有考虑到,WA!(测试数据时本应该测试到的(如:glxlg),吸取教训)
printf("YES\n");
else if(cnt==1)
printf("YES\n");
else
printf("NO\n");
return 0;
}