Description

Given an N × M matrix, your task is to find the number of occurences of an X × Y pattern.

Input
The first line contains a single integer t (t ≤ 15), the number of test cases.
For each case, the first line contains two integers N and M (N, M ≤ 1000). The next N lines
contain M characters each.
The next line contains two integers X and Y (X, Y ≤ 100). The next X lines contain Y characters
each.

Output

For each case, output a single integer in its own line, the number of occurrences.

Sample Input

2
1 1
x
1 1
y
3 3
abc
bcd
cde
2 2
bc
cd


Sample Output
0
2

【题意】

  在二维文本串T中查找一个二维模板串P出现了多少次。

【分析】

  拆分模板串P的每一行,建AC自动机。

  拆分文本串T的每一行,在自动机中与P匹配,ct[i][j]表示以点(i,j)为左上角、与P等大的矩形有多少个对应的行与P匹配。
  最后ct[i][j]==P的行数的i,j就是一个匹配点,ans++。
  

  注意:1.原本我在trie的叶子用动态数组维护了一个表示这一行是P的第几行的数组,但是超时了,后来看了LRJ的代码,改成了用一个nt[i]来表示重复的行的下一行,就A了。
  2.注意在ct[i][j]里加的时候判断i,j是否大于0.

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1010
#define Maxl 110
#define INF 0xfffffff int n,m,l,r;
int ct[Maxn][Maxn],nt[Maxl];
char s[Maxn][Maxn];
char ss[Maxn]; struct node
{
int fail,mark;
int son[];
}t[Maxn*Maxn];int tot; void upd(int x)
{
t[x].mark=;
memset(t[x].son,,sizeof(t[x].son));
} void read_trie(int tk)
{
scanf("%s",s[]+);
int len=strlen(s[]+);
for(int i=;i<=len;i++) ss[i]=s[][len-i+];
int now=;
for(int i=;i<=len;i++)
{
int ind=ss[i]-'a'+;
if(!t[now].son[ind])
{
t[now].son[ind]=++tot;
upd(tot);
}
now=t[now].son[ind];
if(i==len)
{
if(t[now].mark) nt[tk]=t[now].mark;//我好搞笑
t[now].mark=tk;
}
}
} queue<int > q;
void build_AC()
{
while(!q.empty()) q.pop();
q.push();
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=;i<=;i++)
{
if(t[x].son[i])
{
t[t[x].son[i]].fail=x?t[t[x].fail].son[i]:;
q.push(t[x].son[i]);
}
else t[x].son[i]=t[t[x].fail].son[i];
}
if(t[t[x].fail].mark) t[x].mark=t[t[x].fail].mark;
}
} void add(int x,int y,int z)
{
if(x-z+>=) ct[x-z+][y]++;
if(nt[z]!=) add(x,y,nt[z]);
} void ffind()
{
int now;
memset(ct,,sizeof(ct));
for(int i=;i<=n;i++)
{
now=;
for(int j=m;j>=;j--)
{
now=t[now].son[s[i][j]-'a'+];
if(t[now].mark) add(i,j,t[now].mark);
}
}
int ans=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(ct[i][j]==l) ans++;
printf("%d\n",ans);
} void init()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%s",s[i]+);
tot=;upd();
scanf("%d%d",&l,&r);
memset(nt,,sizeof(nt));
for(int i=;i<=l;i++)
{
read_trie(i);
}
build_AC();
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
ffind();
}
return ;
}

[UVA11019]

2016-07-12 15:37:53

【UVA11019】Matrix Matcher的更多相关文章

  1. 【BZOJ4128】Matrix BSGS+hash

    [BZOJ4128]Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...

  2. 【RS】Matrix Factorization Techniques for Recommender Systems - 推荐系统的矩阵分解技术

    [论文标题]Matrix Factorization Techniques for Recommender Systems(2009,Published by the IEEE Computer So ...

  3. 【poj2155】Matrix(二维树状数组区间更新+单点查询)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  4. 【数学】Matrix Multiplication

                                 Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  5. 【UVA11082】Matrix Decompressing(有上下界的网络流)

    题意:给出一个矩阵前i列所有元素的和,和前j行所有元素的和,求这个矩阵解压以后的原型.(答案不唯一) n,m<=20,1<=a[i,j]<=20 思路:这道题把边上的流量作为原先矩阵 ...

  6. 【BNUOJ19500】 Matrix Decompressing

    https://www.bnuoj.com/v3/problem_show.php?pid=19500 (题目链接) 题意 给出一个R行C列的正整数矩阵,设前${A_i}$项为其前i行所有元素之和,$ ...

  7. 【题解】Matrix BZOJ 4128 矩阵求逆 离散对数 大步小步算法

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4128 大水题一道 使用大步小步算法,把数字的运算换成矩阵的运算就好了 矩阵求逆?这么基础的线 ...

  8. 题解【POJ2155】Matrix

    Description Given an \(N \times N\) matrix \(A\), whose elements are either \(0\) or \(1\). \(A[i, j ...

  9. 【poj3233】 Matrix Power Series

    http://poj.org/problem?id=3233 (题目链接) 题意 给出一个n×n的矩阵A,求模m下A+A2+A3+…+Ak 的值 Solution 今日考试就A了这一道题.. 当k为偶 ...

随机推荐

  1. Oracle 插入数据效率对比

    oracle插入数据有多种方式: 将从多个表中查出来的数据插入到临时表中 数据行数 5189597 1.传统方式:直接将数据插入到表中 insert into LLB_BASIC_USER_D_TEM ...

  2. ArcGIS 在地图上添加标注

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. [GDI+] 生成缩略图的类文件SmallImage (转载)

    直接看代码吧,大家可以直接复制使用 /// <summary> /// 类说明:SmallImage类, /// 编码日期:2012-08-20 /// 编 码 人: 苏飞 /// 联系方 ...

  4. android 微信分享没反应问题总结

     一.废话 我必须说我再这个上面吃了很多的亏,所以希望有人不跟我一样吃亏.因为我本身不够仔细的原因,所以我希望能够做一些总结.---废话讲完. 这个文章已经过时了.是几年前写的.http://www. ...

  5. 使用DataList 分页方法

    什么是DataList我想应该不需要解释了,接下来分享本人在项目里使用到的通过DataList进行分页展示方法. 首先在ASPX页面添加一个DataList(后面都简称DL)控件,示例代码如下: &l ...

  6. 深入了解shell

    接触linux很久了,但一直没有总线,老是尝鲜,什么都想学,但好多没多没有记住,特的总结了一些基本的东西,查了很多资料,不完善的方面我会慢慢的更新……   操作系统与外部最主要的接口就叫做shell. ...

  7. Oracle学习第三讲

    关联查询 笛卡尔积 指做关联操作的每个表的每一行都和其他表的每一行组合,假设两个表的记录条数分别为x和y,笛卡尔积将返回x*y条记录 例如:select count(*) from emp; sele ...

  8. Oracle11g服务及实例

    1Orcl服务说明 1) Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写入服务,VSS(Volume Shadow Copy Service)能够让存储基础设备 ...

  9. 这次是C#中的接口

    接口的出现,是为了解决C#中不允许多重继承的问题. 1.什么是接口? 我觉得可以把接口理解为对一组方法声明进行的统一命名,但这些方法没有提供任何实现. 通过接口,就可以对方法进行统一管理,避免了在每种 ...

  10. 吐槽:Lambda表达式

    前面我曾经讨论过Lambda表达式(也就是匿名表达式)的用法, 这里我就主要强调一下匿名表达式的好处. 首先是不需要写多余的方法体,特别是订阅事件的时候,但是也有一个问题,那就是单个方法会因为匿名表达 ...