UVA 11019 Matrix Matcher(二维矩阵匹配ac自动机)

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

题意:给出一个n×m的矩阵一,在给出一个x×y的矩阵二,求矩阵二在矩阵一中出现的次数。

思路:对于矩阵二的每行建立Trie,并在单词结尾结点记录走到该结点的为行数c(有多个可开数组记录),利用一个co[r][i]数组记录在矩阵一中以(r, i)为矩阵二的右上角,大小与矩阵二相同的矩阵包含的行数。对矩阵一每行均find()一遍,最后扫描co数组,统计值为x的个数。
类似如图

注:AC自动机并不是最优解法,时间效率仍是较低,最优解法为二维hash。

代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <vector>

using namespace std;

const int SIZE = 3e2 + 10;
const int N = 1e4 + 10;
const int NN = 1e3 + 10;

struct AC {
    int ch[N][SIZE];
    int sz;
    int f[N];
    bool ed[N];
    int last[N];
    int vt[N][SIZE];
    int vn[N];
    int co[NN][NN];

    int newnode() {
        memset(ch[sz], 0, sizeof(ch[sz]));
        ed[sz] = false;
        f[sz] = 0;
        last[sz] = 0;
        vn[sz] = 0;
        return sz++;
    }

    void init() {
        memset(co, 0, sizeof(co));
        sz = 0;
        newnode();
    }

    void insert(int id, char *s) {
        int u = 0;
        int len = strlen(s);
        for (int i = 0; i < len; i++) {
            int idx = s[i];
            if (!ch[u][idx])
                ch[u][idx] = newnode();
            u = ch[u][idx];
        }
        ed[u] = true;
        vt[u][vn[u]++] = id;
    }

    void getfail() {
        queue<int> q;
        for (int i = 0; i < SIZE; i++)
            if (ch[0][i])
                q.push(ch[0][i]);

        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (int i = 0; i < SIZE; i++) {
                int v = ch[u][i];
                if (v) {
                    q.push(v);
                    int r = f[u];
                    while (r && !ch[r][i])
                        r = f[r];
                    f[v] = ch[r][i];
                    last[v] = (ed[f[v]] ? f[v] : last[f[v]]);
                }
                else {
                    ch[u][i] = ch[f[u]][i];
                }
            }
        }
    }

    void print(int r, int j, int c) {
        if (j) {
            for (int i = 0; i < vn[j]; i++)
                if (r >= vt[j][i])
                    co[r - vt[j][i]][c]++;
            print(r, last[j], c);
        }
    }

    void find(int r, char *s) {
        int u = 0;
        int len = strlen(s);
        for (int i = 0; i < len; i++) {
            int idx = s[i];
            u = ch[u][idx];
            if (ed[u]) print(r, u, i);
            else if (last[u]) print(r, last[u], i);
        }
    }

}ac;

char Mat[NN][NN];

int main() {

    int t_case;
    scanf("%d", &t_case);
    for (int i_case = 1; i_case <= t_case; i_case++) {
        int n, m, x, y;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++)
            scanf("%s", Mat[i]);
        ac.init();
        scanf("%d%d", &x, &y);
        for (int i = 1; i <= x; i++) {
            char str[SIZE];
            scanf("%s", str);
            if (x <= n && y <= m)
                ac.insert(i, str);
        }
        int ans = 0;
        if (x <= n && y <= m) {
            ac.getfail();
            for (int i = 1; i <= n; i++)
                ac.find(i, Mat[i]);

            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    if (ac.co[i][j] == x)
                        ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值