二维的矩阵匹配,把模式矩阵按列拆开构造AC自动机,记录行号(为了缩点判断)。

把T矩阵按行匹配,一旦匹配成功,在假想的子矩阵左上角位置加一。最后统计总数。

因为所有模式串长度一样,不用维护last数组。

模式串可能有重复,结点要用vector来存。

HASH出奇迹,快得不行。。。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = ,maxm = ;
  4. char Text[maxn][maxn], pattern[maxm];
  5. const int maxnds = maxm*maxm, sigma_size = ;
  6. int nds;
  7. int ch[maxnds][sigma_size];
  8. int f[maxnds];
  9. vector<int> val[maxnds];
  10.  
  11. void bfs()
  12. {
  13. f[] = ;
  14. queue<int> q;
  15. for(int c = ; c < sigma_size; c++){
  16. int u = ch[][c];
  17. if(u){ q.push(u); f[u] = ;}
  18. }
  19. while(q.size()){
  20. int r = q.front(); q.pop();
  21. for(int c = ; c < sigma_size; c++){
  22. int u = ch[r][c];
  23. if(!u) { ch[r][c] = ch[f[r]][c]; continue; }
  24. q.push(u);
  25. int v = f[r];
  26. while(v && !ch[v][c]) v = f[v];
  27. f[u] = ch[v][c];
  28. }
  29. }
  30. }
  31.  
  32. #define idx(x) x-'a';
  33. void add(char *s,int i)
  34. {
  35. int n = strlen(s), u = ;
  36. for(int i = ; i < n; i++){
  37. int c = idx(s[i]);
  38. if(!ch[u][c]){
  39. u = ch[u][c] = nds++;
  40. memset(ch[u],,sizeof(ch[u]));
  41. val[u].clear();
  42. }else u = ch[u][c];
  43. }
  44. val[u].push_back(i);
  45. }
  46.  
  47. void init()
  48. {
  49. nds = ;
  50. memset(ch[],,sizeof(ch[]));
  51. val[].clear();
  52. }
  53.  
  54. int cnt[maxn][maxn];
  55. int N,M;
  56. int X,Y;
  57.  
  58. void cal(int i,int j)
  59. {
  60. if(i>=) cnt[i][j]++;
  61. }
  62.  
  63. void Find(char *T,int R)
  64. {
  65. int n = strlen(T), u = ;
  66. for(int i = ; i < n; i++){
  67. int c = idx(T[i]);
  68. u = ch[u][c];
  69. if(val[u].size() ){
  70. for(auto it:val[u]){
  71. cal(R-it+,i-Y+);
  72. }
  73. }
  74. }
  75. }
  76.  
  77. int main()
  78. {
  79. //freopen("in.txt","r",stdin);
  80. int T;cin>>T;
  81. while(T--){
  82. init();
  83. scanf("%d%d",&N,&M);
  84. for(int i = ; i < N; i++){
  85. scanf("%s",Text[i]);
  86. }
  87. scanf("%d%d",&X,&Y);
  88. for(int i = ; i <= X; i++){
  89. scanf("%s",pattern);
  90. add(pattern,i);
  91. }
  92. bfs();
  93. for(int i = ; i < N; i++){
  94. Find(Text[i],i);
  95. }
  96. int ans = ;
  97. for(int i = ; i < N; i++){
  98. for(int j = ; j < M; j++){
  99. if(cnt[i][j]){
  100. if(cnt[i][j] == X) ans++;
  101. cnt[i][j] = ;
  102. }
  103. }
  104. }
  105. printf("%d\n",ans);
  106. }
  107. return ;
  108. }

UVA11019 Matrix Matcher (AC自动机)的更多相关文章

  1. UVA11019 Martix Matcher --- AC自动机

    UVA11019 Martix Matcher 题目描述: 给定一个\(n*m\)的文本串 问一个\(x*y\)的模式串出现的次数 AC自动机的奇妙使用 将\(x*y\)的模式串拆分成x个串,当x个串 ...

  2. UVA11019 Matrix Matcher【hash傻逼题】【AC自动机好题】

    LINK1 LINK2 题目大意 让你在一个大小为\(n*m\)的矩阵中找大小是\(x*y\)的矩阵的出现次数 思路1:Hash hash思路及其傻逼 你把一维情况扩展一下 一维是一个bas,那你二维 ...

  3. UVA11019 Matrix Matcher

    思路 AC自动机匹配二维模式串的题目 因为如果矩形匹配,则每一行都必须匹配,考虑对于一个点,设count[i][j]记录以它为左上角的与模式矩形大小相同的矩形中有多少行和模式矩形匹配 然后把模式矩形的 ...

  4. Aho-Corasick automaton(AC自动机)解析及其在算法竞赛中的典型应用举例

    摘要: 本文主要讲述了AC自动机的基本思想和实现原理,如何构造AC自动机,着重讲解AC自动机在算法竞赛中的一些典型应用. 什么是AC自动机? 如何构造一个AC自动机? AC自动机在算法竞赛中的典型应用 ...

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

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

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

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

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

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

  8. 【UVA11019】Matrix Matcher

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

  9. 【uva11019-Matrix Matcher】AC自动机+优化+记录

    http://acm.hust.edu.cn/vjudge/problem/33057 题意:在二维文本串T中查找一个二维模板串P出现了多少次. 题解: 拆分模板串P的每一行,建AC自动机.拆分文本串 ...

随机推荐

  1. 设置Mvc路由Asp.net 与 mvc同用

    App_start/RouteConfig.cs/RegisterRoutes(RouteConllection routes) { routes.IgnoreRoute("{resourc ...

  2. Oracle数据库恢复之resetlogs

    实验环境:RHEL 5.4 + Oracle 11.2.0.3 如果是一名合格的Oracle DBA,对resetlogs这种关键字都应该是极其敏感的,当确认需要这种操作时一定要三思而后行,如果自己不 ...

  3. 基于testcontainers的现代化集成测试进阶之路

    大型的软件工程项目除了大量的产品级代码外必不可少的还有大量的自动化测试.自动化测试包含从前端到后端甚至到产品线上不同模块和环境的各种类型的测试.一个比较经典的关于自动化测试分布的理论就是测试金字塔,是 ...

  4. appium自动化测试框架——封装获取设备信息类

    在上一节中,我们已经解决了如何在python中执行cmd,并获取执行结果.下面就小小实战一下,获取设备信息. 一.思路 1.windows上获取设备信息的方法 输入dos命令“adb devices” ...

  5. LeetCode.908-最小差值 1(Smallest Range I)

    这是悦乐书的第348次更新,第372篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第213题(顺位题号是908).给定一个整数数组A,对于每个整数A[i],我们可以选择任 ...

  6. Java 多线程高并发编程 笔记(二)

    1. 单例模式(在内存之中永远只有一个对象) 1.1 多线程安全单例模式——不使用同步锁 public class Singleton { private static Singleton sin=n ...

  7. Unity 打包PC和安卓的路径注意事项

    if UNITY_STANDALONE_WIN || UNITY_EDITOR return Application.persistentDataPath + "/LocalData&quo ...

  8. 学习笔记之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( ...

  9. 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 ...

  10. 069 Sqrt(x) 求平方根

    实现 int sqrt(int x) 函数.计算并返回 x 的平方根.x 保证是一个非负整数.案例 1:输入: 4输出: 2案例 2:输入: 8输出: 2说明: 8 的平方根是 2.82842..., ...