0514 CF#798A Mike and palindrome

本文解析了CodeForces平台上A级难度的一道题目——通过修改一个字符使字符串成为回文串的问题。提供了详细的解题思路及C语言实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘要:判断改变给定字符串的一个字符后,该字符串是否是回文。

题目链接: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
Input Output
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值