1344: PIPI的字符串问题Ⅱ

这道题使用了字符串哈希,模数为ull自然溢出
c199fb2ae3b1837ddb32e2c7ea80d0af

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N=1e6+10, base=23333;
ull hs1[N],hs2,pw=1;
char s[N],t[N];
int main(){
	scanf("%s%s",s+1,t+1);
	int n= strlen(s+1),m=strlen(t+1);
	for(int i=1;i<=n;i++){
		hs1[i]=hs1[i-1]*base+s[i];
	}
	for(int i=1;i<=m;i++){
		hs2=hs2*base+t[i];
		pw*=base;
	}
	int ans=0;
	for(int i=m;i<=n;i++){
		if(hs1[i]-hs1[i-m]*pw==hs2)
			ans++;
	}
	if(ans){
		printf("YES\n%d",ans);
	}else{
		printf("NO");
	}
	return 0;
}
posted @ 2026-02-26 01:38  peter_shen  阅读(9)  评论(0)    收藏  举报