代码
#include <bits/stdc++.h>
using namespace std;
const int M=1005;
using ull=unsigned long long;
const ull P1=131,P2=233;
string s1[M],s2[M];
ull num[M][M],base1[M],base2[M];
void init() {
base1[0]=base2[0]=1;
for(int i=1;i<M;i++) {
base1[i]=base1[i-1]*P1;
base2[i]=base2[i-1]*P2;
}
}
void build(int n,int m,string s[]) {
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)
num[i][j]=num[i-1][j]*P1+num[i][j-1]*P2-num[i-1][j-1]*P1*P2+s[i][j];
}
ull get(int x1,int y1,int x2,int y2) {
return num[x2][y2]-num[x1-1][y2]*base1[x2-x1+1]-num[x2][y1-1]*base2[y2-y1+1]
+num[x1-1][y1-1]*base1[x2-x1+1]*base2[y2-y1+1];
}
int main() {
init();
int TT;cin>>TT;
while(TT--) {
int ans=0;
int n,m;cin>>n>>m;
for(int i=1;i<=n;i++) {
cin>>s1[i];
s1[i]='?'+s1[i];
}
int x,y;cin>>x>>y;
for(int i=1;i<=x;i++) {
cin>>s2[i];
s2[i]='?'+s2[i];
}
build(x,y,s2);
int tmp=num[x][y];
build(n,m,s1);
for(int i=1;i+x-1<=n;i++)for(int j=1;j+y-1<=m;j++)
if(tmp==get(i,j,i+x-1,j+y-1))ans++;
cout<<ans<<'\n';
}
return 0;
}
标签:Matrix,int,Matcher,s1,ull,s2
From: https://www.cnblogs.com/basicecho/p/17060819.html