链接: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自动机 二维文本串查找二维模式串的更多相关文章

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

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

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

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

  3. UVa 11019 Matrix Matcher - Hash

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

  4. uva 11019 Matrix Matcher

    题意:给出一个n*m的字符矩阵T,你的任务是找出给定的x*y的字符矩阵P在T中出现了多少次. 思路:要想整个矩阵匹配,至少各行都得匹配.所以先把P的每行看做一个模式串构造出AC自动机,然后在T中的各行 ...

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

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

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

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

  7. UVA 11019 Matrix Matcher(哈希)

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

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

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

  9. UVA - 11019 Matrix Matcher hash+KMP

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

随机推荐

  1. 【LeetCode】132. Palindrome Partitioning II

    Palindrome Partitioning II  Given a string s, partition s such that every substring of the partition ...

  2. 【java设计模式】之 代理(Proxy)模式

    代理模式的核心作用就是通过代理,控制对对象的访问.这跟实际中是一样的,比如说明星都有经纪人,这就是一个代理,比如有人要找某明星拍戏,那么首先处理这事的是他的经纪人,虽然拍戏需要自己拍,但是拍戏前后的一 ...

  3. APP流氓大法之apk 静默安装

    老大要我弄个自动更新,要用到静默安装,网上找到了些大拿的代码,我拿去改吧改吧,先贴出来: /** * 软件静默安装 * @param apkAbsolutePath apk文件所在路径 * @retu ...

  4. Android 加新的页面

    工程右键->New->Other->Android Activity

  5. Linux内核“问题门” - 学习问题、经验集锦

    陈宪章说:“学贵有疑,小疑则小进,大疑则大进.疑者,觉悟之机也,一番觉悟一番长进.” 培根说:“多问的人将多得.” 还在学校的时候导师在激情讲演之后对着会议室里形态各异但均静默不语的我们痛心疾首的说: ...

  6. uva 116 - Unidirectional TSP (动态规划)

    第一次做动规题目,下面均为个人理解以及个人方法,状态转移方程以及状态的定义也是依据个人理解.请过路大神不吝赐教. 状态:每一列的每个数[ i ][ j ]都是一个状态: 然后定义状态[ i ][ j ...

  7. 博客目录之C#

    C# BackgroundWorker的Bug??? C# BeginInvoke和EndInvoke方法 c# 高效的线程安全队列ConcurrentQueue C# ManualResetEven ...

  8. 深度学习图像标注工具VGG Image Annotator (VIA)使用教程

    VGG Image Annotator (VIA)是一款开源的图像标注工具,由Visual Geometry Group开发. 可以在线和离线使用,可标注矩形.圆.椭圆.多边形.点和线.标注完成后,可 ...

  9. Googleplay从服务器检索信息时出错。[DF-DFERH-01]

    googleapis.cn services.googleapis.cn 两个地址强制走代理就OK了.

  10. 使用matlab处理图像的基础知识

    MATLAB基本函数一 矩阵运算 1.基本算数运算(加减乘除) + -运算要求矩阵维数相同,例m*n * /运算,例A=B*C,B矩阵是m*n矩阵,B是n*p矩阵,则A是m*p矩阵 A/B相当于A*i ...