AcWing 138. 兔子与兔子(字符串hash)

题目链接:点击这里
在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MOD = 10000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 1000010, base = 131;

char str[maxn];
ull h[maxn], p[maxn];

//O(1)查询[L,R]子串的哈希值 
ull get(int l, int r)
{
	return h[r] - h[l-1] * p[r-l+1];
}

int main()
{
	scanf("%s", str + 1);
	int n = strlen(str + 1);
	
	p[0] = 1;
	for(int i = 1; i <= n; ++i)
	{
		h[i] = h[i-1] * base + str[i] - 'a' + 1;
		p[i] = p[i-1] * base;
	}
	
	int m;
	scanf("%d", &m);
	while(m--)
	{
	    int l1, r1, l2, r2;
	    scanf("%d%d%d%d", &l1, &r1, &l2, &r2);
	    if(get(l1,r1)==get(l2,r2))  printf("Yes\n");
	    else    printf("No\n");
	}
	return 0;
}
发布了727 篇原创文章 · 获赞 111 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/104274931