BZOJ 2351 Matrix (Hash)
来源:互联网 发布:怎么查询淘宝客订单 编辑:程序博客网 时间:2024/06/13 21:14
2351: [BeiJing2011]Matrix
Time Limit: 20 Sec Memory Limit: 128 MB
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
Sample Output
1
0
1
HINT
对于100%的实际测试数据,M、N ≤ 1000,Q = 1000
对于40%的数据,A = 1。
对于80%的数据,A ≤ 10。
对于100%的数据,A ≤ 100。
思路:
首先将原矩阵哈希,将所有a*b的子矩阵的哈希值插入哈希表,然后对于每个矩阵哈希之后去哈希表中查找。
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define M 1010 #define base1 10000007 #define base2 10000013 #define mod 10007 using namespace std; struct EE{ unsigned num; int next;}ste[M*M]; unsigned int mix[M][M], pow1[M], pow2[M]; int m, n, a, b, q, idc; int head[20000]; void hs(unsigned int x){//保存hash值,链表避免冲突 int pos = x % mod; ste[++idc].num = x; ste[idc].next = head[pos]; head[pos] = idc; } bool find(unsigned int x){ int pos = x % mod; for(int i=head[pos]; i; i=ste[i].next) if(ste[i].num == x) return true; return false; } int main(){ scanf("%d%d%d%d", &m, &n, &a, &b); for(int i=1; i<=m; i++) for(int j=1; j<=n; j++) scanf("%1d", &mix[i][j]); for(int i=1; i<=m; i++) for(int j=1; j<=n; j++) mix[i][j] += mix[i-1][j] * base1; for(int i=1; i<=m; i++) for(int j=1; j<=n; j++) mix[i][j] += mix[i][j-1] * base2; pow1[0] = pow2[0] = 1; for(int i=1; i<1005; i++){ pow1[i] = pow1[i-1] * base1; pow2[i] = pow2[i-1] * base2; } for(int i=a; i<=m; i++)//原矩阵中所有ab矩阵的hash for(int j=b; j<=n; j++){ unsigned int temp; temp = mix[i][j] - mix[i-a][j]*pow1[a] - mix[i][j-b]*pow2[b] + mix[i-a][j-b]*pow1[a]*pow2[b]; hs( temp ); } scanf("%d", &q); while( q-- ){ for(int i=1; i<=a; i++) for(int j=1; j<=b; j++) scanf("%1d", &mix[i][j]); for(int i=1; i<=a; i++)//计算当前矩阵的hash for(int j=1; j<=b; j++) mix[i][j] += mix[i-1][j]*base1; for(int i=1; i<=a; i++) for(int j=1; j<=b; j++) mix[i][j] += mix[i][j-1]*base2; unsigned int temp = mix[a][b]; if( find(temp) ) puts("1"); else puts("0"); } return 0;}
阅读全文
0 0
- BZOJ 2351 Matrix (Hash)
- bzoj 2351: [BeiJing2011]Matrix(二维Hash)
- BZOJ 2351 BeiJing2011 Matrix Hash
- BZOJ-2462&&2351 矩阵模板&&Matrix 暴力 or Hash orAC自动机 or KMP
- [BZOJ]2351 Matrix
- BZOJ 2351: [BeiJing2011]Matrix
- BZOJ 3098 hash killer 2 (hash)
- BZOJ 2351: [BeiJing2011]Matrix 哈希
- UVA 11019 Matrix Matcher (hash+kmp)
- bzoj 3555: [Ctsc2014]企鹅QQ (hash)
- bzoj 2084: [Poi2010]Antisymmetry (hash+二分)
- bzoj 3574: [Hnoi2014]抄卡组 (字符串Hash)
- bzoj 2803: [Poi2012]Prefixuffix(双Hash)
- bzoj 2462 [BeiJing2011]矩阵模板 (hash)
- bzoj 3207(主席树+hash)
- bzoj4128 Matrix BSGS&hash
- UVA 11019 Matrix Matcher(字符串hash+KMP)
- HDU 4029 Distinct Sub-matrix(hash + 后缀数组)
- element-ui的输入框总结
- Android 问题|注意项
- Python 全局变量与global关键字
- Guava Cache实现本地缓存
- 写个Demo 来理解 Matrix
- BZOJ 2351 Matrix (Hash)
- 【android】onInterceptTouchEvent与onTouchEvent
- HDU6033Add More Zero
- CAD入门 调整虚线比例
- 分析网卡通过i2c总线从e2prom中读取mac地址过程
- 浏览器内核及js引擎
- chorme flash无法使用问题
- JavaScript基础第一章
- C