UVA 11019 Matrix Matcher 矩阵匹配器 AC自动机 二维文本串查找二维模式串
链接:https://vjudge.net/problem/UVA-11019
lrjP218 matrix matcher
#include<bits/stdc++.h>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = ;
const int mod = 1e9+;
const int maxnode = *+;
const int sigma_size = ;
vector<int> as[maxnode];
int cnt[][], ans;
int n, m, x, y;
struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
int f[maxnode];
int last[maxnode];
void clear(){sz = ; memset(ch[],,sizeof ch[]); }
int idx(char c){return c-'a'; } void insert(char *s,int x)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], , sizeof ch[sz]);
val[sz] = ;
as[sz].clear();
ch[u][c] = sz++;
}
u = ch[u][c];
}
as[u].push_back(x);///存储给字符结尾,有哪些行是终点。
val[u] = ;
} void find(char *T,int row){
int n = strlen(T);
int j = ;
for(int i = ; i < n; i++){
int c = idx(T[i]);
//while(j&&!ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) print(j,i,row);
else if(last[j]) print(last[j],i,row);
}
} void print(int j,int col,int row)
{
if(j){
//cnt[val[j]]++;
int len = as[j].size();
for(int i = ; i < len; i++){
if(row-as[j][i]>=){///cnt[row-as[j][i]][col]++这样当前找到的行,不会对自己起作用。而是对以前的起作用。
if(++cnt[row-as[j][i]][col]==x){///因为时间卡的紧,所以我没有在最外面判断。
///在这里判断节约时间。否则超时。
ans++;
} }
}
print(last[j],col,row);
}
} void getFail(){
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++){
int u = ch[][c];
if(u){f[u] = ; q.push(u); last[u] = ;}
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; c++){
int u = ch[r][c];
if(!u){
ch[r][c] = ch[f[r]][c]; continue;
}//if(!u) continue;
q.push(u);
int v = f[r];
while(v&&!ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} } ac ;
char t[][];
char s[];
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
ms(cnt,);
ac.clear();
for(int i = ; i < n; i++){
scanf("%s",t[i]);
}
scanf("%d%d",&x,&y);
for(int i = ; i < x; i++){
scanf("%s",s);
ac.insert(s,i);
}
ac.getFail();
ans = ;
for(int i = ; i < n; i++){
ac.find(t[i],i);
}
cout<<ans<<endl;
}
return ;
} /*
2
1 1
x
1 1
y
3 3
abc
bcd
cde
2 2
bc
cd */
UVA 11019 Matrix Matcher 矩阵匹配器 AC自动机 二维文本串查找二维模式串的更多相关文章
- UVA 11019 Matrix Matcher(ac自动机)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 11019 Matrix Matcher ( 二维字符串匹配, AC自动机 || 二维Hash )
题目: 传送门 题意: 给你一个 n * m 的文本串 T, 再给你一个 r * c 的模式串 S: 问模式串 S 在文本串 T 中出现了多少次. 解: 法一: AC自动机 (正解) 670ms 把模 ...
- 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*m的矩阵,和一个x*y的模式矩阵,求模式矩阵在原矩阵中的出现次数. 看上去是kmp在二维情况下的版本,但单纯的kmp已经无法做到了,所以考虑字符串哈希. 类比一维情况下的哈希算法,利用容斥 ...
- AC自动机(二维) UVA 11019 Matrix Matcher
题目传送门 题意:训练指南P218 分析:一行一行的插入,一行一行的匹配,当匹配成功时将对应子矩阵的左上角位置cnt[r][c]++;然后统计 cnt[r][c] == x 的数量 #include ...
- UVA 11019 Matrix Matcher(哈希)
题意 给定一个 \(n\times m\) 的矩阵,在给定一个 \(x\times y\) 的小矩阵,求小矩阵在大矩阵中出现的次数. \(1 \leq n,m \leq 1000\) \(1\leq ...
- UVA 11019 Matrix Matcher(二维hash + 尺取)题解
题意:在n*m方格中找有几个x*y矩阵. 思路:二维hash,总体思路和一维差不太多,先把每行hash,变成一维的数组,再对这个一维数组hash变成二维hash.之前还在想怎么快速把一个矩阵的hash ...
- UVA - 11019 Matrix Matcher hash+KMP
题目链接:传送门 题解: 枚举每一行,每一行当中连续的y个我们hash 出来 那么一行就是 m - y + 1个hash值,形成的一个新 矩阵 大小是 n*(m - y + 1), 我们要找到x*y这 ...
随机推荐
- 【LeetCode】13. Roman to Integer (2 solutions)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- Linux命令-安全复制命令:scp
scp是有Security的文件copy,基于ssh登录.操作起来比较方便,比如要把当前一个文件copy到远程另外一台主机上. 命令格式: scp [可选参数] 源文件 目标文件 scp 本地文件 远 ...
- 自定义Microsoft Visual Studio 代码模板,增加公司和个人信息
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp目录里面有各种新建模板分类: 修 ...
- SQL Server里面导出SQL脚本(表数据的insert语句)
转载自:http://hi.baidu.com/pigarmy/blog/item/109894c445eab0a28326ac5a.html 最近需要导出一个表的数据并生成insert语句,发现SQ ...
- C#中遍历DataTable类型并删除行数据
从数据库中读取出了DataSet类型的数据,通过dataSet.Tables[0]获得DataTable类型的数据. 这时候如果想批量修改dataTable中的内容,比如要删除dataTable中co ...
- Python练习笔记——通讯录查询V1.0
作业: 编写一个代码,实现手机通讯录管理 实现功能:增.删.改.查 字典,列表 [扩展]不要求一定做出来 实现拼音首字母查找 phone = {} while True: num = input(&q ...
- OAF_OAF增删改-新增的实现(案例)
2014-09-14 Created By BaoXinjian
- hdu 1280 前m大的数 哈希
前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- java基础常见问题和eclipse常用快捷键
一.java常用库 java.lang中 StringBuffer,StringBuilder,System,Runtime,Math java.util Date,Calendar,Random j ...
- Linux数据库:MYSQL启用和查看二进制日志
在/etc/my.cnf文件中[mysqld]下加上: server-id = 1 (在整个Mysql集群中保证唯一) log-bin = binlog log-bin-index = binlog ...