题意:给定上一n*m的矩阵,然后的t个p*q的小矩阵,问你匹配不上的有多少个。

析:可以直接用哈希,也可以用AC自动机解决。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #define debug() puts("++++");
  18. #define gcd(a, b) __gcd(a, b)
  19. #define lson l,m,rt<<1
  20. #define rson m+1,r,rt<<1|1
  21. #define freopenr freopen("in.txt", "r", stdin)
  22. #define freopenw freopen("out.txt", "w", stdout)
  23. using namespace std;
  24.  
  25. typedef long long LL;
  26. typedef unsigned long long ULL;
  27. typedef pair<int, int> P;
  28. const int INF = 0x3f3f3f3f;
  29. const LL LNF = 1e16;
  30. const double inf = 0x3f3f3f3f3f3f;
  31. const double PI = acos(-1.0);
  32. const double eps = 1e-8;
  33. const int maxn = 1000 + 10;
  34. const int mod = 1e9 + 7;
  35. const int dr[] = {-1, 0, 1, 0};
  36. const int dc[] = {0, 1, 0, -1};
  37. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  38. int n, m;
  39. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  40. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  41. inline bool is_in(int r, int c){
  42. return r >= 0 && r < n && c >= 0 && c < m;
  43. }
  44. int p, q;
  45. const ULL B1 = 123;
  46. const ULL B2 = 9973;
  47. char s[maxn][maxn], ch[maxn][maxn];
  48. ULL Hash[maxn][maxn], tmp[maxn][maxn];
  49. ULL t1, t2;
  50.  
  51. void solve(char s[][maxn], int n, int m){
  52. for(int i = 0; i < n; ++i){
  53. ULL e = 0;
  54. for(int j = 0; j < q; ++j) e = e * B1 + s[i][j];
  55. for(int j = 0; j + q <= m; ++j){
  56. tmp[i][j] = e;
  57. if(j + q < m) e = e * B1 - t1 * s[i][j] + s[i][j+q];
  58. }
  59. }
  60.  
  61. for(int j = 0; j + q <= m; ++j){
  62. ULL e = 0;
  63. for(int i = 0; i < p; ++i) e = e * B2 + tmp[i][j];
  64. for(int i = 0; i + p <= n; ++i){
  65. Hash[i][j] = e;
  66. if(i + p < n) e = e * B2 - t2 * tmp[i][j] + tmp[i+p][j];
  67. }
  68. }
  69. }
  70.  
  71. int main(){
  72. int t, kase = 0;
  73. while(scanf("%d %d %d %d %d", &n, &m, &t, &p, &q) == 5 && n+m+p+q+t){
  74. for(int i = 0; i < n; ++i) scanf("%s", s+i);
  75. multiset<ULL> sets;
  76. t1 = t2 = 1;
  77. for(int i = 0; i < q; ++i) t1 *= B1;
  78. for(int i = 0; i < p; ++i) t2 *= B2;
  79. for(int i = 0; i < t; ++i){
  80. for(int j = 0; j < p; ++j)
  81. scanf("%s", ch+j);
  82. solve(ch, p, q);
  83. sets.insert(Hash[0][0]);
  84. }
  85. solve(s, n, m);
  86. for(int i = 0; i + p <= n; ++i)
  87. for(int j = 0; j + q <= m; ++j)
  88. sets.erase(Hash[i][j]);
  89. printf("Case %d: %d\n", ++kase, t-(int)sets.size());
  90. }
  91. return 0;
  92. }

  

POJ 3690 Constellations (哈希)的更多相关文章

  1. [poj] 3690 Constellations || 矩阵hash

    原题 在大矩阵里找有几个小矩阵出现过,多组数据 将t个矩阵hash值放入multiset,再把大矩阵中每个hash值从multiset里扔出去,这样最后剩在multiset里的值就是没有找到的小矩阵, ...

  2. TTTTTTTTTTTTTTTTTTTTT POJ 3690 0与* 二维哈希 模板 +multiset

    Constellations Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5923   Accepted: 1164 De ...

  3. POJ 2002 Squares 哈希

    题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...

  4. 【poj3690】Constellations 哈希

    传送门 题目分析 考虑将大矩阵的每个1*q矩阵哈希值求出,然后让小矩阵的第一行在大矩阵中找,如果找到,并且能匹配所有行则出现过.否则没出现过. 在初始化1*q矩阵时可以进行优化:假设该行为123456 ...

  5. poj 2774 字符串哈希求最长公共子串

    Long Long Message #include <iostream> #include <algorithm> #include <cstdio> #incl ...

  6. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

  7. [转] POJ字符串分类

    POJ 1002 - 487-3279(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=1002题意:略解法:二叉查找数,map,快排... POJ 1 ...

  8. POJ3690:Constellations(二维哈希)

    Constellations Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6822   Accepted: 1382 题目 ...

  9. [POJ 1635] Subway tree systems (树哈希)

    题目链接:http://poj.org/problem?id=1635 题目大意:给你两棵树的dfs描述串,从根节点出发,0代表向深搜,1代表回溯. 我刚开始自己设计了哈希函数,不知道为什么有问题.. ...

随机推荐

  1. jsonpath对json数据进行分析校验做接口测试

    在做接口测试的时候, 我们需要对返回的数据进行分析校验, 一般返回的都是json格式的数据, 怎么来解析校验呢? 之前有看过使用递归遍历json数据的, 然后找到了jsonpath, 可以很方便的对j ...

  2. 《thinking in java》 接口与内部类

    书本上的例子 改编后的. package test2; class A{ interface B{void f();} public class BImp implements B{public vo ...

  3. BioNLP概述

    BioNLP概述 工具: GENIA Tagger:GENIA Tagger是一个主要应用于生物医学文本领域的词性标注和浅层语法分析工具,GENIA Tagger在GENIA语料上的词性标记性能F-s ...

  4. CentOS下查看文件和文件夹大小

    当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择. df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力. 当磁盘大小超过标准时会有报警提示,这时如 ...

  5. Codeforces 486D Valid Sets:Tree dp【n遍O(n)的dp】

    题目链接:http://codeforces.com/problemset/problem/486/D 题意: 给你一棵树,n个节点,每个节点的点权为a[i]. 问你有多少个连通子图,使得子图中的ma ...

  6. spring学习(3)

    bean的声明周期 为什么把生命周期当做一个重点? Servlet->servlet生命周期 Servlet生命周期分为三个阶段: 1:初始化阶段,调用init()方法 2:响应客户请求阶段,调 ...

  7. php函数decbin

    decbin()将十进制转换为二进制.必须有一个十进制参数.

  8. Linux-NoSQL之MongoDB

    1.mongodb介绍 什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoD ...

  9. cocos2dx & cocostudio 实现模态对话框

    用cocos2dx实现模态对话框 http://www.cnblogs.com/mrblue/(转自于) ui部分使用了cocoStudio,注意这里没有实现怎么屏蔽其他的输入事件,其他的文档已经太多 ...

  10. 【二叉树的递归】06填充每个节点中的下一个正确的指针【Populating Next Right Pointers in Each Node】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树 struct Tr ...