# BZOJ2462[Beijing2011]矩阵模板(二维Hash)

 1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
5 typedef unsigned int ull;
6 typedef long long ll;
7 using namespace std;
8
9 const int N=1010,P1=2,P2=9191891;
10 int n,m,r,c,Q;
11 ull a[N][N],pw1[N],pw2[N],tot,hs[1000500];
12 char mp[N][N];
13
14 int main(){
15     freopen("bzoj2462.in","r",stdin);
16     freopen("bzoj2462.out","w",stdout);
17     scanf("%d%d%d%d",&n,&m,&r,&c); pw1[0]=pw2[0]=1;
18     rep(i,1,1000) pw1[i]=pw1[i-1]*P1,pw2[i]=pw2[i-1]*P2;
19     rep(i,1,n) scanf("%s",mp[i]+1);
20     rep(i,1,n) rep(j,1,m) a[i][j]=a[i][j-1]*P1+mp[i][j]-'0';
21     rep(i,1,n) rep(j,1,m) a[i][j]=a[i-1][j]*P2+a[i][j];
22     rep(i,r,n) rep(j,c,m)
23         hs[++tot]=a[i][j]-a[i][j-c]*pw1[c]-a[i-r][j]*pw2[r]+a[i-r][j-c]*pw1[c]*pw2[r];
24     sort(hs+1,hs+tot+1); memset(a,0,sizeof(a));
25     for (scanf("%d",&Q); Q--; ){
26         rep(i,1,r) scanf("%s",mp[i]+1);
27         rep(i,1,r) rep(j,1,c) a[i][j]=a[i][j-1]*P1+mp[i][j]-'0';
28         rep(i,1,r) rep(j,1,c) a[i][j]=a[i-1][j]*P2+a[i][j];
29         int k=lower_bound(hs+1,hs+tot+1,a[r][c])-hs;
30         if (hs[k]==a[r][c]) puts("1"); else puts("0");
31     }
32     return 0;
33 }

posted @ 2018-10-27 00:09  HocRiser  阅读(...)  评论(...编辑  收藏