UVA - 11019 Matrix Matcher hash+KMP
题目链接:传送门
题解:
枚举每一行,每一行当中连续的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的更多相关文章
- UVa 11019 Matrix Matcher - Hash
题目传送门 快速的vjudge传送门 快速的UVa传送门 题目大意 给定两个矩阵S和T,问T在S中出现了多少次. 不会AC自动机做法. 考虑一维的字符串Hash怎么做. 对于一个长度为$l$的字符串$ ...
- UVA 11019 Matrix Matcher 矩阵匹配器 AC自动机 二维文本串查找二维模式串
链接:https://vjudge.net/problem/UVA-11019lrjP218 matrix matcher #include<bits/stdc++.h> using na ...
- UVA 11019 Matrix Matcher(二维hash + 尺取)题解
题意:在n*m方格中找有几个x*y矩阵. 思路:二维hash,总体思路和一维差不太多,先把每行hash,变成一维的数组,再对这个一维数组hash变成二维hash.之前还在想怎么快速把一个矩阵的hash ...
- UVA 11019 Matrix Matcher ( 二维字符串匹配, AC自动机 || 二维Hash )
题目: 传送门 题意: 给你一个 n * m 的文本串 T, 再给你一个 r * c 的模式串 S: 问模式串 S 在文本串 T 中出现了多少次. 解: 法一: AC自动机 (正解) 670ms 把模 ...
- UVA - 11019 Matrix Matcher (二维字符串哈希)
给你一个n*m的矩阵,和一个x*y的模式矩阵,求模式矩阵在原矩阵中的出现次数. 看上去是kmp在二维情况下的版本,但单纯的kmp已经无法做到了,所以考虑字符串哈希. 类比一维情况下的哈希算法,利用容斥 ...
- UVA 11019 Matrix Matcher(ac自动机)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- AC自动机(二维) UVA 11019 Matrix Matcher
题目传送门 题意:训练指南P218 分析:一行一行的插入,一行一行的匹配,当匹配成功时将对应子矩阵的左上角位置cnt[r][c]++;然后统计 cnt[r][c] == x 的数量 #include ...
- uva 11019 Matrix Matcher
题意:给出一个n*m的字符矩阵T,你的任务是找出给定的x*y的字符矩阵P在T中出现了多少次. 思路:要想整个矩阵匹配,至少各行都得匹配.所以先把P的每行看做一个模式串构造出AC自动机,然后在T中的各行 ...
- UVA 11019 Matrix Matcher(哈希)
题意 给定一个 \(n\times m\) 的矩阵,在给定一个 \(x\times y\) 的小矩阵,求小矩阵在大矩阵中出现的次数. \(1 \leq n,m \leq 1000\) \(1\leq ...
随机推荐
- java面向抽象编程样例
import java.util.*; abstract class Geometry{ public abstract double getArea(); } class Pillar ...
- HDU 1071 The area ——微积分
[题目分析] 求二次函数和一次函数围成的面积. 先解方程求出一次函数和二次函数. 然后积分. 现在还是不会积分. [代码] #include <cstdio> #include <c ...
- [BZOJ1596] [Usaco2008 Jan]电话网络(树形DP || 贪心)
传送门 1.树形DP #include <cstdio> #include <cstring> #include <iostream> #define N 1000 ...
- BZOJ 1800: [Ahoi2009]fly 飞行棋【暴力】
Description 给出圆周上的若干个点,已知点与点之间的弧长,其值均为正整数,并依圆周顺序排列. 请找出这些点中有没有可以围成矩形的,并希望在最短时间内找出所有不重复矩形. Input 第一行为 ...
- Hibernate 笔记 HQL查询 条件查询,聚集函数,子查询,导航查询
在hibernate中进行多表查询,每个表中各取几个字段,也就是说查询出来的结果集并没有一个实体类与之对应,如何解决这个问题? 解决方案一,按照Object[]数据取出数据,然后自己组bean 解决方 ...
- Codeforces Round #287 (Div. 2) D. The Maths Lecture [数位dp]
传送门 D. The Maths Lecture time limit per test 1 second memory limit per test 256 megabytes input stan ...
- IOS开发之触摸背景关闭键盘的代码实现
直接上代码: // 触摸背景,关闭键盘 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch ...
- JFinal Weixin 1.6发布【转】
原文:http://www.oschina.net/news/69495/jfinal-weixin-1-6-released#rd 继JFinal 2.1发布之后,再来一发JFinal Weixin ...
- FireDac心得
usesFireDAC.Phys.MySQL, FireDAC.Stan.Def, FireDAC.DApt, FireDAC.Comp.Client, FireDAC.Comp.UI, FireDA ...
- Rmq Problem
大视野——3339: Rmq Problem Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1192 Solved: 620[Submit][Sta ...