UVA11019 Matrix Matcher (AC自动机)
二维的矩阵匹配,把模式矩阵按列拆开构造AC自动机,记录行号(为了缩点判断)。
把T矩阵按行匹配,一旦匹配成功,在假想的子矩阵左上角位置加一。最后统计总数。
因为所有模式串长度一样,不用维护last数组。
模式串可能有重复,结点要用vector来存。
HASH出奇迹,快得不行。。。
- #include<bits/stdc++.h>
- using namespace std;
- const int maxn = ,maxm = ;
- char Text[maxn][maxn], pattern[maxm];
- const int maxnds = maxm*maxm, sigma_size = ;
- int nds;
- int ch[maxnds][sigma_size];
- int f[maxnds];
- vector<int> val[maxnds];
- void bfs()
- {
- f[] = ;
- queue<int> q;
- for(int c = ; c < sigma_size; c++){
- int u = ch[][c];
- if(u){ q.push(u); f[u] = ;}
- }
- while(q.size()){
- 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];
- }
- }
- }
- #define idx(x) x-'a';
- void add(char *s,int i)
- {
- int n = strlen(s), u = ;
- for(int i = ; i < n; i++){
- int c = idx(s[i]);
- if(!ch[u][c]){
- u = ch[u][c] = nds++;
- memset(ch[u],,sizeof(ch[u]));
- val[u].clear();
- }else u = ch[u][c];
- }
- val[u].push_back(i);
- }
- void init()
- {
- nds = ;
- memset(ch[],,sizeof(ch[]));
- val[].clear();
- }
- int cnt[maxn][maxn];
- int N,M;
- int X,Y;
- void cal(int i,int j)
- {
- if(i>=) cnt[i][j]++;
- }
- void Find(char *T,int R)
- {
- int n = strlen(T), u = ;
- for(int i = ; i < n; i++){
- int c = idx(T[i]);
- u = ch[u][c];
- if(val[u].size() ){
- for(auto it:val[u]){
- cal(R-it+,i-Y+);
- }
- }
- }
- }
- int main()
- {
- //freopen("in.txt","r",stdin);
- int T;cin>>T;
- while(T--){
- init();
- scanf("%d%d",&N,&M);
- for(int i = ; i < N; i++){
- scanf("%s",Text[i]);
- }
- scanf("%d%d",&X,&Y);
- for(int i = ; i <= X; i++){
- scanf("%s",pattern);
- add(pattern,i);
- }
- bfs();
- for(int i = ; i < N; i++){
- Find(Text[i],i);
- }
- int ans = ;
- for(int i = ; i < N; i++){
- for(int j = ; j < M; j++){
- if(cnt[i][j]){
- if(cnt[i][j] == X) ans++;
- cnt[i][j] = ;
- }
- }
- }
- printf("%d\n",ans);
- }
- return ;
- }
UVA11019 Matrix Matcher (AC自动机)的更多相关文章
- UVA11019 Martix Matcher --- AC自动机
UVA11019 Martix Matcher 题目描述: 给定一个\(n*m\)的文本串 问一个\(x*y\)的模式串出现的次数 AC自动机的奇妙使用 将\(x*y\)的模式串拆分成x个串,当x个串 ...
- UVA11019 Matrix Matcher【hash傻逼题】【AC自动机好题】
LINK1 LINK2 题目大意 让你在一个大小为\(n*m\)的矩阵中找大小是\(x*y\)的矩阵的出现次数 思路1:Hash hash思路及其傻逼 你把一维情况扩展一下 一维是一个bas,那你二维 ...
- UVA11019 Matrix Matcher
思路 AC自动机匹配二维模式串的题目 因为如果矩形匹配,则每一行都必须匹配,考虑对于一个点,设count[i][j]记录以它为左上角的与模式矩形大小相同的矩形中有多少行和模式矩形匹配 然后把模式矩形的 ...
- Aho-Corasick automaton(AC自动机)解析及其在算法竞赛中的典型应用举例
摘要: 本文主要讲述了AC自动机的基本思想和实现原理,如何构造AC自动机,着重讲解AC自动机在算法竞赛中的一些典型应用. 什么是AC自动机? 如何构造一个AC自动机? AC自动机在算法竞赛中的典型应用 ...
- UVA 11019 Matrix Matcher 矩阵匹配器 AC自动机 二维文本串查找二维模式串
链接:https://vjudge.net/problem/UVA-11019lrjP218 matrix matcher #include<bits/stdc++.h> using na ...
- 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 把模 ...
- 【UVA11019】Matrix Matcher
Description Given an N × M matrix, your task is to find the number of occurences of an X × Y pattern ...
- 【uva11019-Matrix Matcher】AC自动机+优化+记录
http://acm.hust.edu.cn/vjudge/problem/33057 题意:在二维文本串T中查找一个二维模板串P出现了多少次. 题解: 拆分模板串P的每一行,建AC自动机.拆分文本串 ...
随机推荐
- 设置Mvc路由Asp.net 与 mvc同用
App_start/RouteConfig.cs/RegisterRoutes(RouteConllection routes) { routes.IgnoreRoute("{resourc ...
- Oracle数据库恢复之resetlogs
实验环境:RHEL 5.4 + Oracle 11.2.0.3 如果是一名合格的Oracle DBA,对resetlogs这种关键字都应该是极其敏感的,当确认需要这种操作时一定要三思而后行,如果自己不 ...
- 基于testcontainers的现代化集成测试进阶之路
大型的软件工程项目除了大量的产品级代码外必不可少的还有大量的自动化测试.自动化测试包含从前端到后端甚至到产品线上不同模块和环境的各种类型的测试.一个比较经典的关于自动化测试分布的理论就是测试金字塔,是 ...
- appium自动化测试框架——封装获取设备信息类
在上一节中,我们已经解决了如何在python中执行cmd,并获取执行结果.下面就小小实战一下,获取设备信息. 一.思路 1.windows上获取设备信息的方法 输入dos命令“adb devices” ...
- LeetCode.908-最小差值 1(Smallest Range I)
这是悦乐书的第348次更新,第372篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第213题(顺位题号是908).给定一个整数数组A,对于每个整数A[i],我们可以选择任 ...
- Java 多线程高并发编程 笔记(二)
1. 单例模式(在内存之中永远只有一个对象) 1.1 多线程安全单例模式——不使用同步锁 public class Singleton { private static Singleton sin=n ...
- Unity 打包PC和安卓的路径注意事项
if UNITY_STANDALONE_WIN || UNITY_EDITOR return Application.persistentDataPath + "/LocalData&quo ...
- 学习笔记之a,b=b,a+b与a=b,b=a+b的区别
兔子序列中用到的常用的计算方法:a,b=b,a+b 当我们真正去运行的时候,会发现它与a=b,b=a+b是有区别的 实例代码如下: def YY(one): a,b,n=0,1,0 while( ...
- HDU-3499:Flight(SPFA+dp)
Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to ...
- 069 Sqrt(x) 求平方根
实现 int sqrt(int x) 函数.计算并返回 x 的平方根.x 保证是一个非负整数.案例 1:输入: 4输出: 2案例 2:输入: 8输出: 2说明: 8 的平方根是 2.82842..., ...