ybt1455模板hash求查错嘤嘤嘤
  • 板块灌水区
  • 楼主qpdk777
  • 当前回复10
  • 已保存回复10
  • 发布时间2020/4/28 09:40
  • 上次更新2023/11/7 03:48:34
查看原帖
ybt1455模板hash求查错嘤嘤嘤
261932
qpdk777楼主2020/4/28 09:40

我不想写的和书上一样,可是为什么我的代码连样例都过不去?

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;

long long int t,n,m,ans;
unsigned long long hash1[10010],hash2[1000010];
string s1,s2;
const unsigned long long int d = 1e9+7,h = 1<<31;

long long int quickpow(int a,int n){
	if(n == 0)
		return 1;
	if(n & 1)
		return a*quickpow(a,n-1)%h;
	else{
		long long int num = quickpow(a,n>>1)%h;
		return num*num%h;
	}
}

int main(){
	ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
	cin>>t;
	while(t--){
		ans = 0;
		cin>>s1>>s2;
		n = s1.length(),m = s2.length();
		s1.insert(0," "),s2.insert(0," ");
		long long int dn = quickpow(d,n);
		for(int i = 1; i <= n; ++i)
			hash1[i] = (hash1[i-1]*d+s1[i]-'A'+1)%h;
		for(int i = 1; i <= m; ++i)
			hash2[i] = (hash2[i-1]*d+s2[i]-'A'+1)%h;
		for(int i = 0; i <= m-n; ++i)
			if(hash1[n] == (hash2[i+n]-hash2[i]*dn)%h)
				++ans;
		cout<<ans<<'\n';
	}
	
	return 0;
}
2020/4/28 09:40
加载中...