炸了的loj,回不来的hash

本文介绍了一种使用C++实现的字符串匹配算法,通过预处理和滚动哈希的方法,提高了字符串匹配的效率。代码中使用了宏定义简化循环和运算,并采用模块化的思想进行设计。通过对输入字符串的预处理,生成了滚动哈希值,实现了快速查找子串的功能。

如题,由于loj炸了
此片代码正确性有待考究

#include<bits/stdc++.h>
#define re return
#define reg register
#define ll long long
#define inc(i,l,r) for(register long long i=l;i<=r;++i)
#define dec(i,l,r) for(register long long i=l;i>=r;--i) 
const int mod=1e9+7,BASE=233,maxn=1e6+5;


/*char buf[1<<21],*p1,*p2;
inline int getc(){re p1==p2 and (p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}


template<typename T>inline void rd(T&x)
{
	char c;bool f=0;
	while((c=getc())<'0'||c>'9')if(c=='-')f=1;
	x=c^48;
	while((c=getc())>='0'&&c<='9')x=(x*10+(c^48))%mod;
	if(f)x=-x;
}*/

using namespace std;
ll power[maxn],sum[maxn],sumb,ans;
char a[maxn],b[maxn];
                                                                                                  

int main()
{
	freopen("in.txt","r",stdin);
	scanf("%s",a+1);scanf("%s",b+1);
	int n=strlen(a+1),m=strlen(b+1);
	power[0]=1;
	inc(i,1,n)power[i]=(power[i-1]*BASE)%mod;
	
	inc(i,1,n)sum[i]=(sum[i-1]*BASE+a[i])%mod;
	
	inc(i,1,m)sumb=(sumb*BASE+b[i])%mod;
	
	inc(i,0,n-m)
	if((sum[i+m]-(sum[i]*power[m])%mod+mod)%mod==sumb)++ans;
	printf("%lld",ans);
	re 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值