【CodeForces - 798A】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

abccaa

Output

YES

Input

abbcca

Output

NO

Input

abcda

Output

YES

题目大意:

麦克有一个只包含小写字母的字符串 s 。他想知道他能否恰好改变这个字符串的一个字母使得这个字符串成为回文串。

回文串是从前向后读与从后向前读相同的串,举个例子, "z", "aaa", "aba", "abccba" 都是回文串,但字符串 "codeforces", "reality", "ab" 就不是。

解题报告:

  分奇偶数讨论就行了,,注意是恰好改变一个字符,,也就是说不能不改。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
char s[MAX];
int main()
{
	cin>>(s+1);
	int len = strlen(s+1);
	int cnt = 0;
	if(len&1) {
		for(int i = 1; i<=(len>>1); i++) {
			if(s[i] != s[len-i+1]) cnt++;
		}
		if(cnt <= 1) puts("YES");
		else puts("NO");
	}
	else {
		for(int i = 1; i<=(len>>1); i++) {
			if(s[i] != s[len-i+1]) cnt++;
		}
		if(cnt == 1) puts("YES");
		else puts("NO");
	}
	return 0 ;
 }

很早之前写的垃圾代码:(我当时也太水了吧)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> 
using namespace std;
char a[20];
char b[20];
int main()
{
	cin>>a;
	int len=strlen(a);
	int flag=0; 
	strcpy(b,a);
	reverse(b,b+len);
//	printf("%d\n",len);
	for(int i = 0; i<len; i++) {
		if(a[i]!=b[i]) {
			b[i]=a[i];
			a[len-1-i]=b[i];
			flag=1;
			break;
		}
	}
//	printf("%s\n%s",a,b);
	if(len&1) {
		for(int i = 0; i<len; i++) {
			if(a[i]!=b[i]) {
				printf("NO");
				return 0 ;
			}
		}
		printf("YES");
		return 0 ;	
	}
	else {
		if(flag==0) {
			printf("NO");
			return 0 ;
		}
		for(int i = 0; i<len; i++) {
			if(a[i]!=b[i]) {
				printf("NO");
				return 0 ;
			}
		}
		printf("YES");
		return 0 ;	
	} 
 } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值