POJ 3690 Constellations (哈希)

题意:给定上一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;
}

  

时间: 2024-08-06 08:51:14

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

poj 3690 Constellations 矩阵的hash

给定一个n*m矩阵和t个p*q的矩阵,求这t个矩阵有多少个是n*m的子矩阵. 矩阵都是01矩阵,只有'0' '*' 矩阵的hash,先将每行q列hash,得到一个新矩阵,然后再每列p行hash [注意行列hash时候取的magic数不能一样,不然很容易冲突,会WA,最好取2个素数] 这样原矩阵的每个子矩阵都由一个数字代替了,之后用map判断就够了. 注意:不能事先将原矩阵的所有子矩阵hash值存在map里,然后比较,由于子矩阵数量太大会MLE. map里存的应该是t个矩阵的hash值,注意有重复

poj 3690 字符矩阵匹配----HASH算法

http://poj.org/problem?id=3690 UVA还有一道也是这样的题,LRJ给的算法是AC自动机----我还没写过,今天用HASH搞了这道题 思路很清晰,但是处理起来还因为HASH函数写混WA了几次... 文本矩阵n*m    T个匹配矩阵p*q 思路: 1.把每一行处理出长为q的hash值 2.对于1中得到的p个哈希值在算一次哈希,这样就把一个矩阵用一个hash值替代了 3.把所有的匹配矩阵压入multiset,然后对于文本矩阵的每一个p*q的子矩阵,算出矩阵哈希值,从mu

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

Constellations Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5923   Accepted: 1164 Description The starry sky in the summer night is one of the most beautiful things on this planet. People imagine that some groups of stars in the sky f

POJ 2774 字符串哈希_解题报告

题目:  http://poj.org/problem?id=2774 A - Long Long Message The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway ticke

POJ 2002 Squares (哈希)

题意:给定 n 个点,问有能组成多少个正方形. 析:通过直接桥梁两个顶点,然后再算另外两个,再通过哈希进行查找另外两个,这里我先是用的map,竟然卡过了3400ms多,后来改成哗哈希,900ms,哈希我也是用STL中的容器来写的,list,先枚举的那两个点是相邻的,然后再通过旋转90度,去计算另外两个. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #inclu

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

Long Long Message #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #include <stack> #include <cmath> #include <map> #include <string&

[转] POJ字符串分类

POJ 1002 - 487-3279(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=1002题意:略解法:二叉查找数,map,快排... POJ 1200 - Crazy Search(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=1200题意:找出不相同的子串数量,字母表大小和子串长度会给定,这题很推荐hash入门者一做解法:hash(建议karp-rabin) POJ 1204 - Word

poj 3274 哈希

http://poj.org/problem?id=3274 Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow down the list of features shared by his cows to a list of only K different features (1 ≤ K ≤ 30). For examp

poj 3349:Snowflake Snow Snowflakes(哈希查找,求和取余法+拉链法)

Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30529   Accepted: 8033 Description You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Y