A

来源:互联网 发布:公司使用的php开发软件 编辑:程序博客网 时间:2024/05/19 04:02

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.
Example
Input
abccaa
Output
YES
Input
abbcca
Output
NO
Input
abcda
Output
YES

对于这种题,思想就是想到那些比较极端的情况,还是说自己的思维不够严密,或者说自己的想法不够科学、有效,所以还是要多见多想。

自己的代码,经过多次缝缝补补,和别人的一比较,还是感觉有差距。
自己的代码

#include<stdio.h>#include<string.h>int main(){        char string[20];        int len,time=0,left,right;        scanf("%s",string+1);        len=strlen(string+1);        if(len%2==0)        {            right=len;            left=1;            while(left<right)            {                if(string[left]!=string[right])                    time++;                right--;                left++;            }            if(time==1)                printf("YES\n");            else                printf("NO\n");        }        else if(len==1)        {            printf("YES\n");        }        else        {            left=1;            right=len;            while(left<right)            {                if(string[left]!=string[right])                    time++;                right--;                left++;            }            if(time==1||time==0)                printf("YES\n");           else                printf("NO\n");        }    return 0;}

别人家的代码

#include<stdio.h>#include<string.h>int main(){        char string[20];        int len,time=0,left,right;        scanf("%s",string+1);        len=strlen(string+1);        for(left=1,right=len;left<right;left++,right--)        {            if(string[left]!=string[right])                time++;        }        if(len%2==0)        {            if(time==1)                printf("YES\n");            else                printf("NO\n");        }        else        {            if(time<=1)                printf("YES\n");            else                printf("NO\n");        }    return 0;}

自己的代码显得还是很臃肿,不想别人的那么行云流水。既能完成功能,还能显得很美观。
还是说说这个题吧,还是一道“水题“(不需要数据结构),大体思想就是两边同时向中间走,看看偶数的话,是否只有一处不同,要是奇数的话,就区分在过程中不同,和最后那个中间无关(当时没有考虑到aaa的情况,也没考虑到aba的情况),导致直接wa掉,还是自己想的不够细,没能仔细的从自己的逻辑出发,只是认为自己的想法没错,不能够静下心来,慢慢的分析漏掉的情况,做题的时候不能过于慌张,这样不好