AcWing 841字符串哈希

在這裏插入圖片描述

思路:字符串哈希

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define space putchar(' ')
#define enter putchar('\n')
const int N=3e5+7;
int a[N],s[N];

  ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b); }

ll lcm(ll a,ll b)
{
    return a*(b/gcd(a,b));
}

template <class T>
void read(T &x){
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
	if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
	x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x){
    if(x < 0) x = -x, putchar('-');
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
int n,m;
ll h[100005],p[100005];
int base=131;
char str[100005];
ll check(int l,int r)
{
   // printf("%lld\n",h[r]-h[l-1]*p[r-l+1]);
    return h[r]-h[l-1]*p[r-l+1];///對齊位數
}
int main()
{
    scanf("%d%d%s",&n,&m,str+1);
    p[0]=1;
    for(int i=1;i<=n;i++)
    {
        p[i]=p[i-1]*base;
        h[i]=h[i-1]*base+str[i];
    }
    while(m--){
        int l1,l2,r2,r1;
        scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
        if(check(l1,r1)==check(l2,r2))
            printf("Yes\n");
        else
            printf("No\n");
    }


    return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章