码迷,mamicode.com
首页 > 其他好文 > 详细

(模板)字符串哈希

时间:2019-04-27 17:04:55      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:lse   algorithm   ring   cstring   cpp   ash   register   字符串   pre   

#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;
}

  

(模板)字符串哈希

标签:lse   algorithm   ring   cstring   cpp   ash   register   字符串   pre   

原文地址:https://www.cnblogs.com/wmq12138/p/10779294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!