A. Mike and palindrome
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard 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
题目链接:http://codeforces.com/contest/798/problem/A
题意:
改变一个字符,让这组字符串变成回文序列,能的话输出YES,不能输出NO
分析:
这道题有点坑,应该是卡了三组数据,aa输出NO,a输出YES,aba也输出YES,估计过了这三组,就可以AC了吧
这题我的做法是分奇偶性,判断条件s[i]==s[len-i-1]是否成立,成立的话ans++,然后判断奇数项长度ans==(len)/2-1||ans==len/2是否成立,成立输出YES,否则输出NO
偶数项长度只需判断ans==(len)/2-1是否成立,成立为YES,否则为NO
下面给出AC代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 char s[20]; 6 while(cin>>s) 7 { 8 int len=strlen(s); 9 for(int i=0;i<len;i++) 10 { 11 for(int j='a';j<='z';j++) 12 { 13 if(s[i]==j) 14 continue; 15 } 16 } 17 int ans=0; 18 if(len%2!=0) 19 { 20 for(int i=0;i<(len)/2;i++) 21 { 22 if(s[i]==s[len-i-1]) 23 ans++; 24 } 25 if(ans==(len)/2-1||ans==len/2) 26 cout<<"YES"<<endl; 27 else cout<<"NO"<<endl; 28 29 } 30 else if(len%2==0) 31 { 32 for(int i=0;i<(len)/2;i++) 33 { 34 if(s[i]==s[len-i-1]) 35 ans++; 36 } 37 if(ans==(len)/2-1) 38 cout<<"YES"<<endl; 39 else cout<<"NO"<<endl; 40 } 41 } 42 return 0; 43 }