zoukankan      html  css  js  c++  java
  • 字符串哈希(模板)

    描述

    给定一个长度为n的字符串,再给定m个询问,每个询问包含四个整数l1,r1,l2,r2,请你判断[l1,r1]和[l2,r2]这两个区间所包含的字符串子串是否完全相同。

    字符串中只包含大小写英文字母和数字。

    输入格式

    第一行包含整数n和m,表示字符串长度和询问次数。

    第二行包含一个长度为n的字符串,字符串中只包含大小写英文字母和数字。

    接下来m行,每行包含四个整数l1,r1,l2,r2,表示一次询问所涉及的两个区间。

    注意,字符串的位置从1开始编号。

    输出格式

    对于每个询问输出一个结果,如果两个字符串子串完全相同则输出“Yes”,否则输出“No”。

    每个结果占一行。

    数据范围

    1 ≤ n , m ≤ 10^5 

    输入样例:

    8 3
    aabbaabb
    1 3 5 7
    1 3 6 8
    1 2 1 2

    输出样例:

    Yes
    No
    Yes

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef unsigned long long ull;//自然溢出
    const int maxn=1e5+7;
    ull base=131;//经验值
    ull p[maxn]; char s[maxn]; map<ull,ull>hs; ull hash(ull l,ull r) { return hs[r]-hs[l-1]*p[r-l+1]; } int main() { int n,m; cin>>n>>m; scanf("%s",s+1); p[0]=1; for(int i=1;i<=n;i++) { hs[i]=hs[i-1]*base+s[i]; p[i]=p[i-1]*base; } int l1,r1,l2,r2; while(m--) { cin>>l1>>r1>>l2>>r2; if(hash(l1,r1)==hash(l2,r2))cout<<"Yes"<<endl; else cout<<"No"<<endl; } }
     
  • 相关阅读:
    [NC13331]城市网络
    Codeforces Round #638 (Div. 2)
    牛客练习赛62
    “科大讯飞杯”第18届上海大学程序设计联赛春季赛暨高校网络友谊赛
    Codeforces Round #635 (Div. 2)
    Codeforces Round #631 (Div. 2)
    牛客每日一题
    Codeforces Round #627 (Div. 3)
    MySQL查看建表语句
    Oracle的number数据类型
  • 原文地址:https://www.cnblogs.com/chuliyou/p/14425593.html
Copyright © 2011-2022 走看看