[模板]AC自动机(1)

题目描述

给定一个文本串和多个模式串,求有几个模式串出现在文本串中

#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 1000005

char s[MAXN];
int N;
struct queue{
    int que[MAXN];int head,tail;
    queue():head(1),tail(0){}
    inline void pop(){head++;}
    inline int front(){return que[head];}
    inline void push(int x){que[++tail]=x;}
    inline bool empty(){return head>tail;}
}q;
struct Auto{

    int trie[26][MAXN],val[MAXN],fail[MAXN],tot;
    Auto():tot(0){}

    inline void ins(){
        int _next = 0;int len = std::strlen(s);
        for(register int i=0;i<len;++i){
            int c = s[i]-'a';
            if(!trie[c][_next])trie[c][_next] = ++tot;
            _next = trie[c][_next];
        }
        val[_next]++;
    }

    inline void make_fail(){
        for(register int i=0;i<26;++i){
            if(trie[i][0]){
                q.push(trie[i][0]);
                fail[trie[i][0]] = 0;
            }
        }
        while(!q.empty()){
            int u = q.front();q.pop();
            for(register int i=0;i<26;++i){
                if(trie[i][u]){
                    q.push(trie[i][u]);
                    fail[trie[i][u]] = trie[i][fail[u]];
                }
                else trie[i][u] = trie[i][fail[u]];
            }
        }
    }

    inline int find(){
        int ans = 0;int len = std::strlen(s);int _next = 0;
        for(register int i=0;i<len;++i){
            _next = trie[s[i]-'a'][_next];
            for(register int pos=_next;pos&&val[pos]!=-1;pos=fail[pos]){
                ans+=val[pos];
                val[pos] = -1;
            }
        }
        return ans;
    }
}AC;

int main(){

    scanf("%d\n",&N);
    for(register int i=1;i<=N;++i){
        scanf("%s",s);
        AC.ins();
    }
    AC.make_fail();
    scanf("%s",s);
    int ans = AC.find();
    printf("%d",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/Neworld2002/p/9499332.html

时间: 2024-10-07 10:25:52

[模板]AC自动机(1)的相关文章

算法模板——AC自动机

实现功能——输入N,M,提供一个共计N个单词的词典,然后在最后输入的M个字符串中进行多串匹配(关于AC自动机算法,此处不再赘述,详见:Aho-Corasick 多模式匹配算法.AC自动机详解.考虑到有时候字典会相当稀疏,所以引入了chi和bro指针进行优化——其原理比较类似于邻接表,这个东西本身和next数组本质上是一致的,只是chi和bro用于遍历某一节点下的子节点,next用于查询某节点下是否有需要的子节点) 1 type 2 point=^node; 3 node=record 4 ex:

模板——AC自动机

#include<bits/stdc++.h> using namespace std; struct nob{ int fail,son[27],ed; }a[1000000]; int cnt=0; void build (string s){ int now=0; for (int i=0; i<s.length(); i++){ if (a[now].son[s[i]-'a']==0) a[now].son[s[i]-'a']=++cnt; now=a[now].son[s[i]

AC自动机例题

P3808 [模板]AC自动机(简单版) [题目描述] 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. #include<bits/stdc++.h> using namespace std; typedef long long LL; const int INF=1e9+7; inline LL read(){ register LL x=0,f=1;register char c=getchar(); while(c<48||c>57){if(c=='-')f=

ac自动机基础模板(hdu2222)

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 long description, when users type some keywords to find the image, th

LA 4670 出现次数最多的子串 (AC自动机模板题)

Dominating Patterns Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; ea

hdu5384 AC自动机模板题,统计模式串在给定串中出现的个数

http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation&q

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

Match:Keywords Search(AC自动机模板)(HDU 2222)

多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. 1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 #include <string.h> 5 #define MAX 26 6 7 using namespace std; 8 9 struct node 10 {

[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