Description
给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在原矩阵中出现过。
所谓01矩阵,就是矩阵中所有元素不是0就是1。
Input
输入文件的第一行为M、N、A、B,参见题目描述。
接下来M行,每行N个字符,非0即1,描述原矩阵。
接下来一行为你要处理的询问数Q。
接下来Q个矩阵,一共Q*A行,每行B个字符,描述Q个01矩阵。
Output
你需要输出Q行,每行为0或者1,表示这个矩阵是否出现过,0表示没有出现过,1表示出现过。
Sample Input
3 3 2 2
111
000
111
3
11
00
11
11
00
11
111
000
111
3
11
00
11
11
00
11
Sample Output
1
0
1
0
1
HINT
对于100%的实际测试数据,M、N ≤ 1000,Q = 1000
对于40%的数据,A = 1。
对于80%的数据,A ≤ 10。
对于100%的数据,A ≤ 100。
Solution
这题我们用hash来做。
Tip:这题是BZOJ2462数据的强化版。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #define UI unsigned int using namespace std; const UI mo=50000,case1=11,case2=9309317; int n,m,A,B,tot,Next[1000100],head[50200]; UI a[1005][1005],f1[1005],f2[1005],b[1005][1005],tree[1000100]; void Insert(UI x) { UI t=x%mo; tot++; Next[tot]=head[t]; head[t]=tot; tree[tot]=x; } bool calc(UI x) { UI t=x%mo; for (int i=head[t];i;i=Next[i]) if (tree[i]==x) return true; return false; } int main() { scanf("%d%d%d%d",&n,&m,&A,&B); tot=0; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) scanf("%1d",&a[i][j]); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) a[i][j]+=a[i][j-1]*case1; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) a[i][j]+=a[i-1][j]*case2; f1[0]=f2[0]=1; for (int i=1;i<=max(n,m);i++) f1[i]=f1[i-1]*case1,f2[i]=f2[i-1]*case2; for (int X=0;X<=n-A;X++) for (int Y=0;Y<=m-B;Y++) { int i=X+A,j=Y+B; UI t=a[i][j]+a[X][Y]*f1[B]*f2[A]-a[X][j]*f2[A]-a[i][Y]*f1[B]; Insert(t); } int q; scanf("%d",&q); while (q--) { for (int i=1;i<=A;i++) for (int j=1;j<=B;j++) scanf("%1d",&b[i][j]); for (int i=1;i<=A;i++) for (int j=1;j<=B;j++) b[i][j]=b[i][j-1]*case1+b[i][j]; for (int i=1;i<=A;i++) for (int j=1;j<=B;j++) b[i][j]=b[i-1][j]*case2+b[i][j]; if (calc(b[A][B])) printf("1\n");else printf("0\n"); } return 0; } |
多写几个字吧啊啊啊
不了,就记录一下。个别题目详细一点吧。