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

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

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int p, q;
const ULL B1 = 123;
const ULL B2 = 9973;
char s[maxn][maxn], ch[maxn][maxn];
ULL Hash[maxn][maxn], tmp[maxn][maxn];
ULL t1, t2; void solve(char s[][maxn], int n, int m){
for(int i = 0; i < n; ++i){
ULL e = 0;
for(int j = 0; j < q; ++j) e = e * B1 + s[i][j];
for(int j = 0; j + q <= m; ++j){
tmp[i][j] = e;
if(j + q < m) e = e * B1 - t1 * s[i][j] + s[i][j+q];
}
} for(int j = 0; j + q <= m; ++j){
ULL e = 0;
for(int i = 0; i < p; ++i) e = e * B2 + tmp[i][j];
for(int i = 0; i + p <= n; ++i){
Hash[i][j] = e;
if(i + p < n) e = e * B2 - t2 * tmp[i][j] + tmp[i+p][j];
}
}
} int main(){
int t, kase = 0;
while(scanf("%d %d %d %d %d", &n, &m, &t, &p, &q) == 5 && n+m+p+q+t){
for(int i = 0; i < n; ++i) scanf("%s", s+i);
multiset<ULL> sets;
t1 = t2 = 1;
for(int i = 0; i < q; ++i) t1 *= B1;
for(int i = 0; i < p; ++i) t2 *= B2;
for(int i = 0; i < t; ++i){
for(int j = 0; j < p; ++j)
scanf("%s", ch+j);
solve(ch, p, q);
sets.insert(Hash[0][0]);
}
solve(s, n, m);
for(int i = 0; i + p <= n; ++i)
for(int j = 0; j + q <= m; ++j)
sets.erase(Hash[i][j]);
printf("Case %d: %d\n", ++kase, t-(int)sets.size());
}
return 0;
}

  

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. JFreeChart的使用示例

    示例一,饼图,简单示例: 导入jar,代码文件: 运行结果: 代码: import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartF ...

  2. Idea 包名按树形结构展示

    Idea默认包名展示如图: 感觉这样展示,在包下面建包的时候不方便,可以在 设置按钮 里面去掉 Flatten Packages 和 Compact Empty Middle Packages,设置如 ...

  3. Docker学习(三)

    查看docker daemon服务运行状态 service docker status

  4. 大话设计模式--职责连模式 Chain of Resposibility -- C++实现实例

    1. 职责链模式: 使多个对象都有机会处理请求,从而避免请求发送者和接受者之间的耦合关系,将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它. 当客户提交一个请求时,请求是沿着链传递直 ...

  5. 记录quick cocos2d-x3.2升级至cocos2d-x3.8

    目前为止,quickcocos2d-x没有3.8版本,想用3.8又想用quick,所以只能自己升级了,自己先记录下,防止忘记. cocos2d-x3.8里面有quick framework,而simu ...

  6. js 处理移动端触摸事件

    在处理移动端的touch事件时,我们可以选择一些插件来处理,比如jquery ui touch punch.js 提供丰富的触摸效果,可以满足移动端的开发, 但是,有些移动端开发中,并不需要如此复杂的 ...

  7. Javascript 模块化编程 --RequireJs

    什么是模块化 模块就是实现特定功能的一组方法,常见的几种js写法 原始写法 function A() { } function B() { } 上面函数A()和B()组成一个模块,使用的时候直接调用就 ...

  8. AMD模块定义规范

    AMD 即Asynchronous Module Definition,中文名是“异步模块定义”的意思.它是一个在浏览器端模块化开发的规范,服务器端的规范是CommonJS.   模块将被异步加载,模 ...

  9. Python基础-数据写入execl

    import xlwt book = xlwt.Workbook()#创建一个excel sheet = book.add_sheet('lanxia')#添加一个sheet页 title = ['姓 ...

  10. linux命令学习笔记(9):touch 命令

    linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. .命令格式: touch [选项]... 文件... .命令参数: -a 或--t ...