题目链接传送门

题解:

  枚举每一行,每一行当中连续的y个我们hash 出来

  那么一行就是 m - y + 1个hash值,形成的一个新 矩阵 大小是 n*(m - y + 1), 我们要找到x*y这个矩阵的 话 是不是就是 在每一列跑kmp就行了?

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 2e3+, M = 1e3+,inf = 2e9; const ULL mod = 10000019ULL;
int n,m,x,y,T,fail[N];
char a[N][N],b[N][N];
ULL has[N][N],mp[N][N],s[N],t[N],sqr[N];
void build_fail() {
int j = ;
memset(fail,,sizeof(fail));
for(int i = ; i <= y; ++i) {
while(j&&s[i]!=s[j+]) j = fail[j];
if(s[i] == s[j+]) j++;
fail[i] = j;
}
}
int kmp() {
int ret = , j = ;
for(int i = ; i <= n; ++i) {
while(j&&s[j+]!=t[i]) j = fail[j];
if(s[j+] == t[i]) j++;
if(j == x) {
ret += ;
j = fail[j];
}
}
return ret;
}
int main() {
sqr[] = ;
for(int i = ; i < N; ++i) sqr[i] = sqr[i-] * mod;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
for(int i = ; i <= n; ++i) scanf("%s",a[i]+);
scanf("%d%d",&x,&y);
for(int i = ; i <= x; ++i) scanf("%s",b[i]+);
if(n < x || m < y) {
puts("");
continue;
}
for(int i = ; i <= n; ++i) {
for(int j = ; j <= m; ++j)
has[i][j] = has[i][j - ] * mod + a[i][j];
}
for(int i = ; i <= x; ++i) {
ULL now = , ps = ;
for(int j = ; j <= y; ++j) {
now = now * mod + b[i][j];
}
s[i] = now;
}
build_fail();
int ans = ;
for(int j = ; j <= m - y + ; ++j) {
for(int i = ; i <= n; ++i) {
int l = j, r = j + y - ;
ULL now = has[i][r] - has[i][l-] * sqr[r - l + ];
t[i] = now;
}
ans += kmp();
}
printf("%d\n",ans);
}
return ;
}

UVA - 11019 Matrix Matcher hash+KMP的更多相关文章

  1. UVa 11019 Matrix Matcher - Hash

    题目传送门 快速的vjudge传送门 快速的UVa传送门 题目大意 给定两个矩阵S和T,问T在S中出现了多少次. 不会AC自动机做法. 考虑一维的字符串Hash怎么做. 对于一个长度为$l$的字符串$ ...

  2. UVA 11019 Matrix Matcher 矩阵匹配器 AC自动机 二维文本串查找二维模式串

    链接:https://vjudge.net/problem/UVA-11019lrjP218 matrix matcher #include<bits/stdc++.h> using na ...

  3. UVA 11019 Matrix Matcher(二维hash + 尺取)题解

    题意:在n*m方格中找有几个x*y矩阵. 思路:二维hash,总体思路和一维差不太多,先把每行hash,变成一维的数组,再对这个一维数组hash变成二维hash.之前还在想怎么快速把一个矩阵的hash ...

  4. UVA 11019 Matrix Matcher ( 二维字符串匹配, AC自动机 || 二维Hash )

    题目: 传送门 题意: 给你一个 n * m 的文本串 T, 再给你一个 r * c 的模式串 S: 问模式串 S 在文本串 T 中出现了多少次. 解: 法一: AC自动机 (正解) 670ms 把模 ...

  5. UVA - 11019 Matrix Matcher (二维字符串哈希)

    给你一个n*m的矩阵,和一个x*y的模式矩阵,求模式矩阵在原矩阵中的出现次数. 看上去是kmp在二维情况下的版本,但单纯的kmp已经无法做到了,所以考虑字符串哈希. 类比一维情况下的哈希算法,利用容斥 ...

  6. UVA 11019 Matrix Matcher(ac自动机)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. AC自动机(二维) UVA 11019 Matrix Matcher

    题目传送门 题意:训练指南P218 分析:一行一行的插入,一行一行的匹配,当匹配成功时将对应子矩阵的左上角位置cnt[r][c]++;然后统计 cnt[r][c] == x 的数量 #include ...

  8. uva 11019 Matrix Matcher

    题意:给出一个n*m的字符矩阵T,你的任务是找出给定的x*y的字符矩阵P在T中出现了多少次. 思路:要想整个矩阵匹配,至少各行都得匹配.所以先把P的每行看做一个模式串构造出AC自动机,然后在T中的各行 ...

  9. UVA 11019 Matrix Matcher(哈希)

    题意 给定一个 \(n\times m\) 的矩阵,在给定一个 \(x\times y\) 的小矩阵,求小矩阵在大矩阵中出现的次数. \(1 \leq n,m \leq 1000\) \(1\leq ...

随机推荐

  1. iRule Event Order - HTTPSv7

    v

  2. Python 频繁读取Mysql相关问题

    1.需要频繁select大量数据,时间长.消耗内存大,如何解决mysql性能问题? 如果对返回的结果数量没有要求,可以控制返回的数量: cursor.fetchmany(size=1000) 这样是只 ...

  3. [BZOJ1575] [Usaco2009 Jan]气象牛Baric(DP)

    传送门 DP f[i][j]表示前i个中选j个的最优解 预处理g[i][j]表示选i~j对答案的贡献 那么就可以n^3乱搞了! 注意边界 #include <cstdio> #includ ...

  4. [luoguP2862] [USACO06JAN]把牛Corral the Cows(二分 + 乱搞)

    传送门 可以二分边长 然后另开两个数组,把x从小到大排序,把y从小到大排序 枚举x,可以得到正方形的长 枚举y,看看从这个y开始,往上能够到达多少个点,可以用类似队列来搞 其实发现算法的本质之后,x可 ...

  5. P2014 选课 (树形动规)

    题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...

  6. cf682E Alyona and Triangles

    You are given n points with integer coordinates on the plane. Points are given in a way such that th ...

  7. scanf printf函数返回值

    1. scanf 函数是有返回值的,它的返回值可以分成三种情况  1) 正整数,表示正确输入参数的个数.例如执行 scanf("%d %d", &a, &b);   ...

  8. 【转】UITableViewCell自适应高度 UILabel自适应高度和自动换行

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {     ...

  9. ORA-01940: cannot drop a user that is currently connected 问题解析

    https://www.linuxidc.com/Linux/2012-12/76448.htm

  10. Codeforces Gym 100286I iSharp 模拟

    原题地址:http://codeforces.com/gym/100286/attachments/download/2013/20082009-acmicpc-northeastern-europe ...