【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
abccaaOutput
YESInput
abbccaOutput
NOInput
abcdaOutput
YES題目大意:
麥克有一個只包含小寫字母的字符串?s?。他想知道他能否恰好改變這個字符串的一個字母使得這個字符串成為回文串。
回文串是從前向后讀與從后向前讀相同的串,舉個例子, "z", "aaa", "aba", "abccba" 都是回文串,但字符串 "codeforces", "reality", "ab" 就不是。
解題報告:
? 分奇偶數(shù)討論就行了,,注意是恰好改變一個字符,,也就是說不能不改。
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 ;}很早之前寫的垃圾代碼:(我當(dāng)時也太水了吧)
#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 ; } }?
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 798A】Mike and palindrome (回文串,水题,字符串问题)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mtdacq.exe - Mtdacq是
- 下一篇: 【HDU - 1559】最大子矩阵 (二