0514 CF#798A Mike and palindrome

来源:互联网 发布:java 格式化当前时间 编辑:程序博客网 时间:2024/06/05 02:12
摘要:判断改变给定字符串的一个字符后,该字符串是否是回文。

题目链接:http://codeforces.com/contest/798/problem/A

A. Mike and palindrome

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.

Examples
InputOutput
abccaa
YES
abbcca
NO
abcda
YES

方法:计算前后对称位置处不同的字符有几个。 可以查一半,也可以全部查。查后分情况处理。
注意几种情况:
-1:已经对称的奇数串
-2:已经对称的偶数串
-3 :差一位回文的串

源代码
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5. char str[20];
  6. scanf("%s",str);
  7. int len=strlen(str);
  8. int i;
  9. int count=0;
  10. for(i=0;i<len;i++)
  11. {
  12. if(str[i]!=str[len-1-i])
  13. count++;
  14. }
  15. if(count==2 || (count==0 && len%2==1))
  16. printf("YES\n");
  17. else printf("NO\n");
  18. return 0;
  19. }

2017-5-14
0 0
原创粉丝点击