UVa11019 Matrix Matcher 留坑[AC自动机]
来源:互联网 发布:淘宝卖土特产前景分析 编辑:程序博客网 时间:2024/05/29 04:30
声明:此题没写,只理解了一下思路,坑留在这里什么时候复习再做。
Given an N M matrix, your task is to nd the number of occurences of an X Y pattern.
Input
The rst line contains a single integer t (t 15), the number of test cases.
For each case, the rst 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
题意:给定二维文本串二维模板串,求匹配数;
分析:二维文本串比较短…所以…其实就是对模板串枚举行逐行匹配,用文本串建AC自动机,这样对于每一行都能知道在哪一行出现过(AC自动机并没有直接把两个单词拆分组合,这一点要注意,每个走出来的单词都是完整的),然后f记录一下再枚举相加就可以了。
标程:
// File Name: 11019.cpp// Author: zlbing// Created Time: 2013/3/23 14:37:07#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#define REP(i,r,n) for(int i=r;i<=n;i++)#define RREP(i,n,r) for(int i=n;i>=r;i--)const int SIGMA_SIZE = 26;const int MAXNODE = 111000;const int MAXS = 150 + 10;map<string,int> ms;//ms是为了满足特殊要求,比如模板串相同时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 d[1005][1005]; void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); ms.clear(); memset(d,0,sizeof(d)); memset(next,0,sizeof(next)); } // 字符c的编号 int idx(char c) { return c-'a'; } // 插入字符串。v必须非0 void insert(char *s, int v) { int u = 0, n = strlen(s); for(int i = 0; i < n; i++) { int c = idx(s[i]); if(!ch[u][c]) { memset(ch[sz], 0, sizeof(ch[sz])); val[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } if(val[u]) { next[v]=val[u]; } val[u] = v; ms[string(s)] = v; } // 递归打印匹配文本串str[i]结尾的后缀,以结点j结尾的所有字符串 void print(int i,int j,int x) { if(j) { if(x-val[j]+1>0) d[x-val[j]+1][i]++; int t=val[j]; while(next[t]) { t=next[t]; if(x-t+1>0) d[x-t+1][i]++; } print(i,last[j],x); } } // 在T中找模板 int find(char* T,int x) { int n = strlen(T); int j = 0; // 当前结点编号,初始为根结点 for(int i = 0; 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[0] = 0; // 初始化队列 for(int c = 0; c < SIGMA_SIZE; c++) { int u = ch[0][c]; if(u) { f[u] = 0; q.push(u); last[u] = 0; } } // 按BFS顺序计算fail while(!q.empty()) { int r = q.front(); q.pop(); for(int c = 0; 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[1005][1005];char str1[105][105];int main(){ int T; scanf("%d",&T); while(T--) { int N,M,X,Y; scanf("%d%d",&N,&M); REP(i,1,N) scanf("%s",str[i]); scanf("%d%d",&X,&Y); REP(i,1,X) scanf("%s",str1[i]); solver.init(); REP(i,1,X){ solver.insert(str1[i],i); } solver.getFail(); REP(i,1,N) { solver.find(str[i],i); } int ans=0; REP(i,1,N) REP(j,1,M) if(solver.d[i][j]==X) ans++; printf("%d\n",ans); } return 0;}
来自:http://www.cnblogs.com/arbitrary/archive/2013/03/23/2977617.html
版权声明同资料整理系列。
- UVa11019 Matrix Matcher 留坑[AC自动机]
- UVA11019 Matrix Matcher AC自动机
- [UVA11019] Matrix Matcher && AC自动机
- 【ac自动机】uva11468 Substring && uva11019 Matrix Matcher
- uva11019 - Matrix Matcher 二维矩阵匹配 AC自动机
- 【Uva11019】Matrix Matcher【AC自动机】【二维字符串匹配】
- 11019 - Matrix Matcher--AC自动机
- UVA11019 Matrix Matcher(一种计算子矩阵在母矩阵中出现次数的办法,AC自动机+kmp)
- Matrix Matcher UVA11019
- UVa11019 Matrix Matcher
- uva11019 Matrix Matcher
- AC自动机 UVa11019
- UVa:11019 Matrix Matcher(AC自动机)
- uva 11019 - Matrix Matcher(AC自动机)
- uva 11019 - Matrix Matcher --AC自动机
- UVA-11019 - Matrix Matcher(AC自动机)
- UVA 1019 Matrix Matcher(AC自动机)
- UVA 11019 Matrix Matcher(AC自动机)
- Android中EditTex焦点设置和弹不弹出输入法的问题
- c libcurl 基本使用
- Android4.4 Camera callback注册和回调过程分析
- studio写jniHelloWorld的步骤
- 基于CentOS7开发之路 --- 第一章 : 安装 CentOS 7.2-1511
- UVa11019 Matrix Matcher 留坑[AC自动机]
- ROS通信原理介绍
- 大数据运维框架(四)--开发篇:自动部署程序的功能实现
- 使用 RandomStringUtils 类来生成随机码/随机数 java生成指定范围的随机数
- 【JavaWeb-22】slf4j和log4j的转化、一对一关系的配置、二级缓存的四种类别、阶段案例注意事项
- Java泛型中<? extends E>和<? super E>的区别
- unity3d抛物线的运动的制作
- HTTP幂等性及注意事项
- 解读TCP 四种定时器