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 ...
随机推荐
- shell-001
for: shell_test #!/bin/bash var= var=$var+ echo $var mkdir only_a_joke shell_joke #!/bin/bash ./shel ...
- Java日志实战及解析
Java日志实战及解析 日志是程序员必须掌握的基础技能之一,如果您写的软件没有日志,可以说你没有成为一个真正意义上的程序员. 为什么要记日志? • 监控代码 • 变量变化情况, ...
- MHA 高可用集群搭建(二)
MHA 高可用集群搭建安装scp远程控制http://www.cnblogs.com/kevingrace/p/5662839.html yum install openssh-clients mys ...
- 【转】Eric's并发用户数估算与Little定律的等价性
转自:http://www.cnblogs.com/hundredsofyears/p/3360305.html 在国内性能测试的领域有一篇几乎被奉为大牛之作的经典文章,一个名叫Eric Man Wo ...
- [UOJ#129][BZOJ4197][Noi2015]寿司晚宴
[UOJ#129][BZOJ4197][Noi2015]寿司晚宴 试题描述 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司 ...
- spring之scope作用域
spring中,bean的作用域有五种类型:默认是单例模式, singleton prototype request session ...
- P1168 中位数 (优先队列,巧解)
题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[3], …, A[2k - 1]的中位数.即前1,3,5,……个数的中位数. 输入 ...
- Spoj-ANTP Mr. Ant & His Problem
Mr. Ant has 3 boxes and the infinite number of marbles. Now he wants to know the number of ways he c ...
- ElasticSearch中Date
ElasticSearch中有时会想要通过索引日期来筛选查询的数据,此时就需要用到日期数学表达式. 比如现在的时间是2024年3月22日中午12点.utc 注意,如果是中国的时间需要加上8个小时! 表 ...
- *AtCoder Regular Contest 094 F - Normalization
$n \leq 200000$的abc字符串,现能进行如下变换零次或若干次:选一个$i<n$且$s_i \neq s_{i+1}$,把$s_i$和$s_{i+1}$替换成abc三个字母中除了这两 ...