AC自动机(二维) UVA 11019 Matrix Matcher
题意:训练指南P218
分析:一行一行的插入,一行一行的匹配,当匹配成功时将对应子矩阵的左上角位置cnt[r][c]++;然后统计 cnt[r][c] == x 的数量
#include <bits/stdc++.h>
using namespace std; const int N = 1e3 + 5;
const int NODE = 1e4 + 5;
const int SIZE = 26;
char mat1[N][N], mat2[105][105];
int cnt[N][N];
int n, m, x, y;
struct AC {
int ch[NODE][SIZE], val[NODE], sz;
int fail[NODE], last[NODE];
vector<int> rows[NODE];
void clear(void) {
memset (ch[0], 0, sizeof (ch[0]));
sz = 1;
for (int i=0; i<NODE; ++i) rows[i].clear ();
}
int idx(char c) {
return c - 'a';
}
void insert(char *P, int v) {
int u = 0;
for (int c, i=0; P[i]; ++i) {
c = idx (P[i]);
if (!ch[u][c]) {
memset (ch[sz], 0, sizeof (ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
rows[u].push_back (v);
}
void build(void) {
queue<int> que; fail[0] = 0;
int u;
for (int c=0; c<SIZE; ++c) {
u = ch[0][c];
if (u) {fail[u] = 0; que.push (u);}
}
while (!que.empty ()) {
int r = que.front (); que.pop ();
for (int c=0; c<SIZE; ++c) {
int &u = ch[r][c];
if (!u) {
u = ch[fail[r]][c];
continue;
}
que.push (u);
int v = fail[r];
while (v && !ch[v][c]) v = fail[v]; //
fail[u] = ch[v][c];
last[u] = val[fail[u]] ? fail[u] : last[fail[u]];
}
}
}
void query(int row) {
int u = 0;
for (int c, i=0; mat1[row][i]; ++i) {
c = idx (mat1[row][i]);
u = ch[u][c];
if (i - y + 1 < 0) continue;
if (val[u]) print (row, i - y + 1, u);
else if (last[u]) print (row, i - y + 1, last[u]);
}
}
void print(int r, int c, int u) {
if (u) {
for (int i=0; i<rows[u].size (); ++i) {
int tr = rows[u][i];
if (r - tr + 1 >= 1) {
cnt[r-tr+1][c]++;
}
}
print (r, c, last[u]);
}
}
}ac; int main(void) {
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d%d", &n, &m);
for (int i=1; i<=n; ++i) {
scanf ("%s", &mat1[i]);
}
ac.clear ();
scanf ("%d%d", &x, &y);
for (int i=1; i<=x; ++i) {
scanf ("%s", &mat2[i]);
ac.insert (mat2[i], i);
}
ac.build ();
memset (cnt, 0, sizeof (cnt));
for (int i=1; i<=n; ++i) {
ac.query (i);
}
int ans = 0;
for (int i=1; i<=n-x+1; ++i) {
for (int j=0; j<=m-y; ++j) {
if (cnt[i][j] == x) ans++;
}
}
printf ("%d\n", ans);
} return 0;
}
AC自动机(二维) UVA 11019 Matrix Matcher的更多相关文章
- UVa 11019 (AC自动机 二维模式串匹配) Matrix Matcher
就向书上说得那样,如果模式串P的第i行出现在文本串T的第r行第c列,则cnt[r-i][c]++; 还有个很棘手的问题就是模式串中可能会有相同的串,所以用repr[i]来记录第i个模式串P[i]第一次 ...
- UVA 11019 Matrix Matcher ( 二维字符串匹配, AC自动机 || 二维Hash )
题目: 传送门 题意: 给你一个 n * m 的文本串 T, 再给你一个 r * c 的模式串 S: 问模式串 S 在文本串 T 中出现了多少次. 解: 法一: AC自动机 (正解) 670ms 把模 ...
- UVA 11019 Matrix Matcher 矩阵匹配器 AC自动机 二维文本串查找二维模式串
链接:https://vjudge.net/problem/UVA-11019lrjP218 matrix matcher #include<bits/stdc++.h> using na ...
- 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& ...
- UVA 11019 Matrix Matcher(二维hash + 尺取)题解
题意:在n*m方格中找有几个x*y矩阵. 思路:二维hash,总体思路和一维差不太多,先把每行hash,变成一维的数组,再对这个一维数组hash变成二维hash.之前还在想怎么快速把一个矩阵的hash ...
- UVa 11019 Matrix Matcher - Hash
题目传送门 快速的vjudge传送门 快速的UVa传送门 题目大意 给定两个矩阵S和T,问T在S中出现了多少次. 不会AC自动机做法. 考虑一维的字符串Hash怎么做. 对于一个长度为$l$的字符串$ ...
- 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 ...
随机推荐
- 2048控制台程序:一份帝国理工C++作业
#include <fstream> #include <vector> #include <iostream> #include <string> u ...
- arcgis
投影的时候两个选择: projected:平面的,以米为单位 另一个:球迷的,以经纬度为单位
- sqlplus 设置
set heading offset line 40001.设置页面显示总行数show pagesize; //首先查看目前的pagesize,默认是14set pagesize 100; //将pa ...
- grep如何忽略.svn目录,以及如何忽略多个目录
grep如何忽略.svn目录,以及如何忽略多个目录 这是我在网上看到的文章,不过里面还有问题,我的不支持,需要更换架包 grep -r 'function_name' * (*表示当前目录下所有文件, ...
- hdu1071(抛物线弓形面积阿基米德算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1071 题意:给出抛物线的顶点和它与一直线的两交点,求他们围成的面积: 思路: 可以直接求出他们的方程式 ...
- .net学习笔记----有序集合SortedList、SortedList<TKey,TValue>、SortedDictionary<TKey,TValue>
无论是常用的List<T>.Hashtable还是ListDictionary<TKey,TValue>,在保存值的时候都是无序的,而今天要介绍的集合类SortedList和S ...
- Vs注释,vsXML,VSXML注释
标签名称 说明 语法 参数 <summary> <summary> 标记应当用于描述类型或类型成员.使用<remarks> 添加针对某个类型说明的补充信息. < ...
- Linux 下 Console / 控制台 复制粘贴快捷键
Linux下复制粘贴快捷键 1. 在终端下: 复制命令:Ctrl + Shift + C 组合键. 粘贴命令:Ctrl + Shift + V 组合键. 2. 在控制台下: 复制命令:Ctrl + ...
- 发现一php木马代码
<?php ;//无需验证密码! $shellname='hello~地球~猴子星球欢迎你 '; define('myaddress',__FILE__); error_reporting(E_ ...
- penghui_031413 Bat命令学习
penghui_031413 Bat命令学习 基础部分:====================================================================== ...