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;
}

發佈了43 篇原創文章 · 獲贊 26 · 訪問量 3979
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章