(模板)字符串哈希

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

#define res register int
/*#define getchar gc
typedef long long LL;

char buf[1<<23],*fc=buf,*tc=buf;
inline char gc()
{
	if(fc==tc)
	{
		tc=(fc=buf)+fread(buf,1,1<<23,stdin);
		if(fc==tc) return EOF;
	}
	return *fc++;
}*/
inline int read()
{
	int x=0,f=1;char ch;
	while(!isdigit(ch=getchar())) if(ch=='-') f=-1;
	while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
	return f*x;
}
const int N=1000000+10;

char s[N];
int len,q;
unsigned long long f[N],p[N];
const unsigned long long P=131;

inline unsigned long long calc(int l,int r)
{
	return f[r]-f[l-1]*p[r-l+1];
}

int main()
{
	scanf("%s",s+1);
	len=strlen(s+1);
	scanf("%d",&q);
	p[0]=1;
	for(res i=1 ; i<=len ; ++i)
	{
		f[i]=f[i-1] * P + (s[i]-'a'+1);//hash of 1~i
		p[i]=p[i-1] * P;//P^i
	}
	for(res i=1 ; i<=q ; ++i)
	{
		int l1,r1,l2,r2;
		scanf("%d %d %d %d",&l1,&r1,&l2,&r2);
		if(calc(l1,r1)==calc(l2,r2))
			puts("Yes");
		else 
			puts("No");
	}
	return 0;
}

  

posted @ 2019-04-27 16:50  孑行  阅读(108)  评论(0编辑  收藏  举报