困难的回文串(哈希模板)

Description
是的没错,学chang就是这么的菜,WA了34次才把这道题过了。题目很简单,就是让你判断一个字符串是否为回文串。仅此而已。
Input
测试数据有多组:
对于每组测试数据输入一个n表示字符串长度,接着输入长度为n的字符串(1 <= n <= 1e7)
Output
对于每组输出判断当前字符串是否为回文串,如果是回文串输出YES,否则输出NO。
Sample Input Copy
6
aaaaaa
6
xuejie
Sample Output Copy
YES
NO

#include<bits/stdc++.h>
using namespace std;
const int mod=1e5;
const int n=131;
char s;
unsigned long long  hs1,hs2;
int len;
int x;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    while(cin>>len)
    {
        hs1 = 0;
        hs2 = 0;
        x=1;
        if(len == 1)
        {
            cin>>s;
            cout<<"YES"<<endl;
            continue;
        }
        for(int i=1; i<=len/2; i++)//正着算
        {
            cin>>s;
            hs1 = (hs1*n+s)%mod;
        }
        if(len & 1)
        {
            cin>>s;
        }
        for(int i=1; i<=len/2; i++)//倒着算
        {
            cin>>s;
            hs2 = (s*x+hs2)%mod;
            x = x*n%mod;
        }
   //     cout<<hs1<<' '<<hs2<<endl;
        if(hs1==hs2) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值