zoukankan      html  css  js  c++  java
  • Mike and palindrome CodeForces

    题目链接

    一个简单的题目,但是却很少有人可以一次AC,比如我就瞎写wa了一次。。。

    写本博算个教训录吧。

    题目给出一个字符串,让你严格的改变一个字符使改变后的字符串是一个回文串。

    回文串不用解释了。不懂自行百度。

    需要注意两点:

    1.如果长度为偶数,并且事先就是一个回文串,那么要输出no的,因为必须要改变一个字符串,在原本就是回文的基础上改变一下就不是回文串了。

    2.如果长度为奇数,并且事先就是一个回文串,那么要输出yes,因为可以只改变最中间的那个字符,改后还是一个回文串。

    其他的就是判断有几对字符不一样了,是一对的话就yes,不是就no。

    我的AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb std::ios::sync_with_stdio(false)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define gg(x) getInt(&x)
    using namespace std;
    typedef long long ll;
    inline void getInt(int* p);
    const int maxn=1000010;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    char s[maxn];
    
    int main()
    {
        scanf("%s",s);
        int len=strlen(s);
        int l=0;
        int cnt=0;
        int r=len-1;
        while(l<=r)
        {
            if(s[l]!=s[r])
            {
                cnt++;
            }
            l++;
            r--;
    
        }
        if(cnt==1||(cnt==0&&(len%2==1)))
            printf("YES
    ");
        else
            printf("NO
    ");
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
    View Code

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    HTML简介
    web服务器的原理
    静态网页和动态网页的区别
    HTTP协议相关原理
    C/S,B/S的应用的区别
    git撤销commit但未push的文件
    表单提交不刷新页面
    httpclient请求中文乱码问题
    web项目,@return@see@param等注解Maven install报错,不能识别
    Hive 基本操作
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10229141.html
Copyright © 2011-2022 走看看