概述
A. Mike and palindrome
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
题目大意:能改变一个字符形成回文就YES,否则NO
解题思路:分奇偶,注意一定要改变。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
while(cin>>s)
{
int len=s.length();
int cnt=0;
if(len<=1) {cout<<"YES"<<endl;continue;}
for(int i=0,j=len-1;i<len/2;i++,j--)
{
if(cnt>1) break;
if(s[i]!=s[j]) cnt++;
}
if((len%2)&&(cnt==0)) cnt++;
if(cnt==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
最后
以上就是无聊冬日为你收集整理的Codeforces798A-Mike and palindrome的全部内容,希望文章能够帮你解决Codeforces798A-Mike and palindrome所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复