Codeforces Round 410 (Div. 2) A - Mike and palindrome 水题

题目连接

http://codeforces.com/problemset/problem/798/A

Description

Mike has a string s consisting of only lowercase English letters. He wants to change 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.

Input

The first and single line contains string s (1 ≤ |s| ≤ 15).

Output

Print “YES” (without quotes) if Mike can change exactly one character so that the resulting string is palindrome or “NO” (without quotes) otherwise.

Sample Input

abccaa

Sample Output

YES

题意

回文串的定义大家都知道,现在给出一个串问你能不能通过一次操作使它变为回文串,而且任何情况下都要操作一次。

题解

WA了两次才发现Trick,这道题一定要注意特判已经是回文串的时候是单数的情况,因为总可以改变中间的字符使它依旧是回文串。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstring>
using namespace std;
char s[20];
int main(){
scanf("%s",s);
int len=strlen(s);
int ans=0;
for(int i=0;i<len-i;++i){
if(s[i]!=s[len-i-1])
ans++;
}
if(ans==1 || len==1 || (ans==0 && len%2))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}