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

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

      

  • 相关阅读:
    hdu 2020
    hdu 1005
    hdu1014
    集合(一)
    史上最全的 Java 新手问题汇总
    表单验证
    oracle优化原则(二)
    vue的学习--如何使用Intellij IDEA配置并运行vue项目
    在win10环境下IED配置spark项目
    在win10环境下配置spark和scala
  • 原文地址:https://www.cnblogs.com/wmq12138/p/10779294.html
Copyright © 2011-2022 走看看