AcWing
  • 首页
  • 题库
  • 题解
  • 分享
  • 问答
  • 活动
  • 应用
  • 吐槽
  • 登录/注册

AcWing 841. 字符串哈希    原题链接    简单

作者: 作者的头像   xhQYm ,  2020-07-10 13:21:48 ,  阅读 170


1


此题解为提供封装版字符串哈希模板,,,可自取

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N=100010,P=131;
int M;
struct Hash
{
    int n;
    char str[N];
    ull h[N],p[N];
    void init()
    {
        p[0]=1;
        for(int i=1;i<=n;i++) h[i]=h[i-1]*P+str[i],p[i]=p[i-1]*P;
    }
    ull get(int l,int r){return h[r]-h[l-1]*p[r-l+1];}
};
Hash h;
int main()
{
    scanf("%d%d",&h.n,&M);
    scanf("%s",h.str+1);
    h.init();
    while(M--)
    {
        int l1,r1,l2,r2;
        scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
        if(h.get(l1,r1)==h.get(l2,r2)) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

复习的时候顺便改成了封装版

0 评论

你确定删除吗?

© 2018-2021 AcWing 版权所有  |  京ICP备17053197号-1
联系我们  |  常见问题
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息