hdu2222 【AC自动机】Keywords Search

题意

给定n个模式串,求目标串中出现了多少个模式串。

传送门

思路

AC自动机模版题。

Code

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e6+10;
struct Ac {
    int tr[maxn][26], fail[maxn], e[maxn], cnt[maxn];
    int tot;
    void init() {
        memset(tr, 0, sizeof(tr));
        memset(e, 0, sizeof(e));
        memset(cnt, 0, sizeof(cnt));
        memset(fail, 0, sizeof(fail));
        tot=0;
    }
    void insert(char *t) {
        int p=0;
        for (int c, i=0; t[i]; ++i) {
            c = t[i]-'a';
            if(!tr[p][c]) tr[p][c] = ++tot;
            p=tr[p][c];
        }
        ++e[p];
    }
    void build() {
        queue<int>q;
        for (int i=0; i<26; ++i) {
            if(tr[0][i])
                q.push(tr[0][i]);
        }
        while(!q.empty()) {
            int u=q.front(); q.pop();
            for (int i=0; i<26; ++i) {
                if(tr[u][i]) fail[tr[u][i]]=tr[fail[u]][i], q.push(tr[u][i]);
                else tr[u][i]=tr[fail[u]][i];
            }
        }
    }
    int query(char *t) {
        int p=0, res=0;
        for (int i=0; t[i]; ++i) {
            p = tr[p][t[i]-'a'];
            for (int j=p; j && e[j]!=-1; j=fail[j]) {
                res += e[j];
                e[j]=-1;
            }
        }
        return res;
    }
}ac;
int n, T;
char str[maxn];

int main() {
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        ac.init();
        for (int i=1; i<=n; ++i) {
            scanf("%s", str);
            ac.insert(str);
        }
        scanf("%s", str);
        ac.build();
        printf("%d\n", ac.query(str));
    }

    return 0;
}

原文地址:https://www.cnblogs.com/acerkoo/p/11397435.html

时间: 2024-08-05 03:52:51

hdu2222 【AC自动机】Keywords Search的相关文章

hdu2222 AC自动机-给定串中出现了几个模式串

http://acm.hdu.edu.cn/showproblem.php?pid=2222 Problem Description In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey also wants to bring this feature to his image retrieval system. Every image have a lo

[hdu2222] [AC自动机模板] Keywords Search [AC自动机]

AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <ctime> 7 #include <cstdlib> 8 #include <queue>

【HDU2222】【Keywords Search】AC自动机,有详细注释题解。

题意:给定N个单词,和一个字符串S,求这N个单词在字符串S中,有多少个出现过. 题解:AC自动机裸题一枚. AC自动机是基于字典树的一种KMP思想高级算法,用于多字串匹配.就是把字典树建好,然后模仿KMP的前缀数组"pre[]",在字典树内处理了一个fail(失败指针),失配时顺着往前找,并寄托于此以得到答案. 直接附代码,里面有详解.(数组模拟版!!!指针神马的都去回收站吧!) 结构体+注释版本: #include <queue> #include <cstdio&

Keywords Search HDU2222 AC自动机模板题

ac自动机说起来很复杂,其实和kmp是一样的思路,都是寻找相同前后缀,减少跳的次数.只要理解了kmp是怎么求next数组的,ac自动机bfs甚至比knp还好写. 这里大致说一下kmp求next数组的方法吧,假设现在要求第c个字符的next值(假设这个c很大,这样画图出来比较清晰方便理解),因为遍历过程中我们已经知道了第c-1个字符的next为x(假设比c小很多),即next[c-1] = x.那就代表我们知道了a[1]-a[x]这一段和a[c-1-x]-a[c-1]这一段是相等的对吧. 那么现在

HDU2222 AC自动机

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 59781    Accepted Submission(s): 19700 Problem Description In the modern time, Search engine came into the life of everybody lik

hdu2222(ac自动机模板)

先推荐两篇写的很好的ac自动机blog: http://blog.csdn.net/creatorx/article/details/71100840 http://blog.csdn.net/niushuai666/article/details/7002823 正题 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意: 给出 n 个模式串以及一个 主串, 问有多少个模式串在主串中出现过 思路: ac自动机模板题 代码: 1 #inc

[hdu2222]ac自动机(模板)

题意:一个文本串+多个模板串的匹配问题 思路:裸的ac自动机. 1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map&g

hdu2222 AC自动机入门

其实很久以来我都把sam当成ac自动机了 ac自动机的建立比sam简单多了 直接暴力建字典树,然后暴力跑fail就好了 (挖个坑:去学fail树) 裸题A一道,岂不美哉 1 #include <bits/stdc++.h> 2 #define MAX 1000000 3 using namespace std; 4 int T,n;char ch; 5 struct acm 6 { 7 int cnt,h,t,c[MAX][26],fail[MAX],que[MAX],ret[MAX]; 8

HDU2222 (AC自动机)

AC自动机模板题. 被卡内存了 死活A不掉.. AC自动机参考教程: http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html 1 const maxn=10008; 2 type arr=record 3 next:array[0..26] of longint; 4 fail,cnt:longint; 5 end; 6 var cas:longint; 7 s:array[0..maxn] of string; 8 T

// hdu2222 // AC自动机初学

// hdu2222 // #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<queue> using namespace std; char k[55],s[1000005]; struct node { int fail; int nex