2610: 判断回文串———PowerOJ

Description
某天吃饭的时候,FM有了一个帅气idea,其实是个傻逼题?题意很简单,给一个字符串判断是否是回文串?
PS:如果一个字符串正着读和反着读都一样,那么这个字符串为回文串,比如aba,cc,cddc为回文串,而ac,acda,adA则不是回文串。

Input
多组输入,第一行为n,代表字符串的长度。
第二行为这个字符串,该串仅包含大小写字母。
Output
是回文串输出"YES", 不是回文串输出 “NO”。
Sample Input
Raw

8
aaaaaaaa
7
tangcan
3
lyl
Sample Output
Raw

YES
NO
YES
Hint
回文串长度
1≤len≤107,请注意内存限制。


题意:中文题自己看

解题思路:字符串hash处理,然后进行比较

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll mod=1e16+7;
const int base=31;
int main()
{
    int n;
    ll h1,h2,mul;
    while(~scanf("%d",&n))
    {
        h1=h2=0;
        mul=1;
        int cnt=1;
        char c;
        getchar();
        while(cnt<=n){
            c=getchar();
            if(cnt<=n/2)
                h1=(h1*base+c)%mod;
            else if(cnt*2-1!=n)
            {
                h2=(h2+mul*c)%mod;
                mul=(mul*base)%mod;
            }
            cnt++;
        }
        if(h1==h2)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44003265/article/details/104037830