题意:给出一个n*m的字符矩阵T,你的任务是找出给定的x*y的字符矩阵P在T中出现了多少次.

思路:要想整个矩阵匹配,至少各行都得匹配。所以先把P的每行看做一个模式串构造出AC自动机,然后在T中的各行逐一匹配,找到P中每一行的所有匹配点。

只要在匹配时做一些附加操作,就可以把匹配出来的单一的行拼成矩形。用一个count[r][c]表示T中一(r,c)为右上角,与P等大的矩形中有多少个 完整的行和P对应位置的行完全相同.当P的第i行出现在T的第r行,起始列编号为c时,意味着count[r-i][c]应当加1.所有匹配结束后,count[r] [c]=X的那些就是一个二维匹配点.

注意:模式串有可能相同,因此需要一个next数组来记录相同的模式串.

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
const int SIGMA_SIZE = ;
const int MAXNODE = ;
const int MAXS = + ; struct ACautomata
{
int ch[MAXNODE][SIGMA_SIZE];
int f[MAXNODE]; // fail函数
int val[MAXNODE]; // 每个字符串的结尾结点都有一个非0的val
int last[MAXNODE]; // 输出链表的下一个结点
int next[MAXS];
int sz;
int count[][];
void init()
{
sz = ;
memset(ch[], , sizeof(ch[]));
memset(count,,sizeof(count));
memset(next,,sizeof(next));
} // 字符c的编号
int idx(char c)
{
return c-'a';
} // 插入字符串。v必须非0
void insert(char *s, int v)
{
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] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
if(val[u])
{
next[v]=val[u];
}
val[u] = v;
} // 递归打印匹配文本串str[i]结尾的后缀,以结点j结尾的所有字符串
void print(int i,int j,int x)
{
if(j)
{
if(x-val[j]+>)
count[x-val[j]+][i]++;
int t=val[j];
while(next[t])
{
t=next[t];
if(x-t+>)
count[x-t+][i]++;
}
print(i,last[j],x);
}
} // 在T中找模板
int find(char* T,int x)
{
int n = strlen(T);
int j = ; // 当前结点编号,初始为根结点
for(int i = ; i < n; i++) // 文本串当前指针
{
int c = idx(T[i]);
j = ch[j][c];
if(val[j]) print(i,j,x);
else if(last[j]) print(i,last[j],x); // 找到了!
}
} // 计算fail函数
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] = ;
}
}
// 按BFS顺序计算fail
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;
}
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]];
}
}
} };
ACautomata solver;
char str[][];
char str1[][];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int N,M,X,Y;
scanf("%d%d",&N,&M);
for(int i=; i<=N; i++)
scanf("%s",str[i]);
scanf("%d%d",&X,&Y);
for(int i=; i<=X; i++)
scanf("%s",str1[i]);
solver.init();
for(int i=; i<=X; i++)
{
solver.insert(str1[i],i);
}
solver.getFail();
for(int i=; i<=N; i++)
{
solver.find(str[i],i);
}
int ans=;
for(int i=; i<=N; i++)
for(int j=; j<=M; j++)
if(solver.count[i][j]==X)
ans++;
printf("%d\n",ans);
}
return ;
}

uva 11019 Matrix Matcher的更多相关文章

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

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

  2. UVa 11019 Matrix Matcher - Hash

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

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

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

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

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

  5. UVA 11019 Matrix Matcher(哈希)

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

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

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

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

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

  8. UVA - 11019 Matrix Matcher hash+KMP

    题目链接:传送门 题解: 枚举每一行,每一行当中连续的y个我们hash 出来 那么一行就是 m - y + 1个hash值,形成的一个新 矩阵 大小是 n*(m - y + 1), 我们要找到x*y这 ...

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

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

随机推荐

  1. 原生js解决跨浏览器兼容问题

    //跨浏览器兼容问题 Util = { //添加类名 add : function(ele,type,hand){ if(ele.addEventListener){ ele.addEventList ...

  2. 【C++基础】sizeof 数组 指针 空NULL

    笔试遇到很多sizeof的小题,博主基础堪忧,怒总结如下,还是要巩固基础啊啊啊! sizeof操作符 对象所占 栈内存空间的大小,单位是字节 关键词:char  数组 指针 结构体 class [注意 ...

  3. 咦,为DJANGO的ORM的QUERYSET增加数据列的样码,很好用哟

    这个我真的没有查资料,是通过直觉和经验弄出来的,哈哈,感觉用深一点好. 这样在模板输出时,就更好控制啦.. if self.kwargs: if self.kwargs.has_key('search ...

  4. 解读Q_GLOBAL_STATIC(QFontDatabasePrivate, privateDb)

    根据 src/corelib/global.h template <typename T>class QGlobalStatic{public: T *pointer; inline QG ...

  5. HDU1312——Red and Black(DFS)

    Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...

  6. open/write/read

    C语言中open函数 作用:打开和创建文件. 简述: 1 2 3 4 #include <fcntl.h> int open(const char *pathname, int flags ...

  7. 【ZOJ】2112 Dynamic Rankings

    树状数组套主席树模板题目. /* 2112 */ #include <iostream> #include <sstream> #include <string> ...

  8. VC 设置 Stack Overflow

    C/C++ stack overflow, 怎样设置stack大小?解决方案 (1) vc6.0: project->setting->link->project options-& ...

  9. centos 如何用 rsyslog 搭建本地日志服务(续1: omprog模块与php deamon的配合使用)

    上一篇说到了如何用 rsyslog 搭建本地的日志服务,地址在这里,没有看的童鞋可以先瞅一眼 : http://www.cnblogs.com/smallrookie/p/5677004.html 显 ...

  10. Missing artifact com.sun:tools:jar:1.5.0的解决方案

    今天在用maven的时候遇到一个问题pom.xml提示Missing artifact com.sun:tools:jar:1.5.0 试过改eclipse的eclipse.ini文件,也试过在ecl ...