# BZOJ2462矩阵模板

2462: [BeiJing2011]矩阵模板
Time Limit: 2 Sec Memory Limit: 128 MB
Submit: 721 Solved: 320
Description

Input

Output

Sample Input
3 3 2 2
111
000
111
3
11
00
11
11
00
11
Sample Output
1
0
1
HINT

Source
Day4

#include<cstdio>
#include<iostream>
using namespace std;
#define MAXN 1001
#define mod 77171
#define base1 9804799
#define base2 9983543
int n,m,A,B,q,hash[mod],tot;
unsigned int a[MAXN][MAXN],b[MAXN][MAXN],pow1[MAXN],pow2[MAXN],Hash;
struct kx
{
unsigned int num;
kx *next;
}s[MAXN*MAXN];

{
int w=0,c=1; char ch=getchar();
while (ch<'0' || ch>'9')
{
if (ch=='-') c=-1;
ch=getchar();
}
while (ch>='0' && ch<='9')
w=w*10+ch-'0',ch=getchar();
return w*c;
}

void insert(unsigned int x)
{
int t=x%mod;
s[++tot].num=x,s[tot].next=&s[hash[t]],hash[t]=tot;
}

bool find(unsigned int x)
{
int t=x%mod;
for (kx *i=&s[hash[t]];i;i=i->next)
if (i->num==x) return 1;
return 0;
}

int main()
{
int i,j;
pow1[0]=pow2[0]=1;
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
scanf("%1d",&a[i][j]);
for (i=1;i<=max(n,m);i++) pow1[i]=pow1[i-1]*base1,pow2[i]=pow2[i-1]*base2;
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
a[i][j]+=a[i-1][j]*base1;
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
a[i][j]+=a[i][j-1]*base2;
for (i=A;i<=m;i++)
for (j=B;j<=n;j++)
{
Hash=a[i][j],Hash-=a[i-A][j]*pow1[A],Hash-=a[i][j-B]*pow2[B],Hash+=a[i-A][j-B]*pow1[A]*pow2[B];
insert(Hash);
}
while (q--)
{
for (i=1;i<=A;i++)
for (j=1;j<=B;j++)
scanf("%1d",&b[i][j]);
for (i=1;i<=A;i++)
for (j=1;j<=B;j++)
b[i][j]+=b[i-1][j]*base1;
for (i=1;i<=A;i++)
for (j=1;j<=B;j++)
b[i][j]+=b[i][j-1]*base2;
if (find(b[A][B])) printf("1\n");
else printf("0\n");
}
return 0;
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客