HDU2896 - 病毒入侵 - AC自动机

病毒侵袭

题目链接

分类data structures strings

1.题意概述

  • 给你N(1<=N<=500)个模式串,M(1<=M<=1000)个待匹配串,查询这M个串每个串中模式串出现过哪几个,和总的出现次数。

2.解题思路

  • 也是裸的AC自动机,因为是有查询几次,为了方便输出,我们插入同时维护记录一下插入的每个模式串的id和次数。

3.AC代码

class Trie {
public:
#define tot_len 100010
#define tot_ch 130
#define CUR s[i]
    int next[tot_len][tot_ch], fail[tot_len], end[tot_len];
    bool used[510];
    int root, L;

    int Newnode() {
        rep(i, 0, tot_ch) next[L][i] = -1;
        end[L++] = -1;
        return L - 1;
    }

    void Init() {
        L = 0;
        root = Newnode();
    }

    void Insert(char *s, int id) {
        int len = strlen(s);
        int now = root;
        rep(i, 0, len) {
            if (next[now][CUR] == -1)
                next[now][CUR] = Newnode();
            now = next[now][CUR];
        }
        end[now] = id;
    }

    void Build() {
        queue<int> q;
        fail[root] = root;
        rep(i, 0, tot_ch) {
            if (next[root][i] == -1)
                next[root][i] = root;
            else {
                fail[next[root][i]] = root;
                q.push(next[root][i]);
            }
        }
        while (!q.empty()) {
            int now = q.front();
            q.pop();
            rep(i, 0, tot_ch) {
                if (next[now][i] == -1)
                    next[now][i] = next[fail[now]][i];
                else {
                    fail[next[now][i]] = next[fail[now]][i];
                    q.push(next[now][i]);
                }
            }
        }
    }

    bool Query(char *s, int n, int id) {
        int len = strlen(s);
        int now = root;
        memset(used, 0, sizeof used);
        bool flag = 0;
        rep(i, 0, len) {
            now = next[now][CUR];
            int tmp = now;
            while (tmp != root) {
                if (end[tmp] != -1) {
                    used[end[tmp]] = 1;
                    flag = 1;
                }
                tmp = fail[tmp];
            }
        }
        if (!flag) return 0;
        printf("web %d:", id);
        rep(i, 1, n + 1) if (used[i]) printf(" %d", i);
        puts("");
        return 1;
    }
#undef CUR
#undef tot_ch
#undef tot_len
} AC;
char ch[maxn], key[291];
inline void solve() {
    int n, m;
    while (~scanf("%d", &n)) {
        AC.Init();
        rep(i, 1, n + 1) {
            scanf("%s", key);
            AC.Insert(key, i);
        }
        AC.Build();
        int ans = 0;
        scanf("%d", &m);
        rep(i, 1, m + 1) {
            scanf("%s", ch);
            if (AC.Query(ch, n, i))
                ans++;
        }
        printf("total: %d\n", ans);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值