题意:给定n个模式串和一个文本串,判断文本中是否存在模式串。


思路:套模板即可。

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 1e6 + 5;
struct Aho{
    struct State{
        int next[26];
        int fail, cnt;
    }state[maxn];
    queue<int>que;
    int size;

    void init() {
        size = 1;
        while(!que.empty()) que.pop();
        for(int i = 0; i < maxn; ++i) {
            memset(state[i].next, 0, sizeof(state[i].next));
            state[i].cnt = state[i].fail = 0;
        }
    }

    void insert(char *S) {
        int n = strlen(S);
        int now = 0;
        for(int i = 0; i < n; ++i) {
            int num = S[i]-'a';
            if(state[now].next[num]) now = state[now].next[num];
            else {
                state[now].next[num] = size++;
                now = state[now].next[num];
            }
        }
        state[now].cnt++;
    }

    void getFail() {
        state[0].fail = -1;
        que.push(0);
        while(!que.empty()) {
            int u = que.front(); que.pop();
            for(int i = 0; i < 26; ++i) {
                if(state[u].next[i]) {
                    if(u == 0) state[state[u].next[i]].fail = 0;
                    else {
                        int v = state[u].fail;
                        while(v != -1 && !state[v].next[i]) v = state[v].fail;
                        if(v == -1) state[state[u].next[i]].fail = -1;
                        else state[state[u].next[i]].fail = state[v].next[i];

                    }
                    que.push(state[u].next[i]);
                }
            }
        }
    }

    int Get(int u) {
        int res = 0;
        while(u) {
            res += state[u].cnt;
            state[u].cnt = 0;
            u = state[u].fail;
        }
        return res;
    }

    bool match(char *S) {
        int n = strlen(S);
        int now = 0, res = 0;
        for(int i = 0; i < n; ++i) {
            int num = S[i]-'a';
            if(state[now].next[num]) now = state[now].next[num];
            else {
                int u = state[now].fail;
                while(u != -1 && !state[u].next[num]) u = state[u].fail;
                if(u == -1) now = 0;
                else now = state[u].next[num];
            }
            if(state[now].cnt) res += Get(now);
            if(res) return true;
        }
        return false;
    }
}aho;

char S[maxn];
int n;
int main() {
    while(scanf("%d", &n) == 1) {
        aho.init();
        for(int i = 0; i < n; ++i) {
            scanf("%s", S);
            aho.insert(S);
        }
        aho.getFail();
        scanf("%s", S);
        if(aho.match(S)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

如有不当之处欢迎指出!

hihoCoder 1036 Trie图 AC自动机的更多相关文章

  1. 1036 : Trie图 (AC自动机)

    题目大意: 输入 n 个目标单词和一个文本串,判断文本串中是否存在某些目标单词. 思路 赤裸裸的 AC自动机. 代码: #include<iostream> #include<cst ...

  2. BZOJ 1444 [JSOI2009]有趣的游戏 (Trie图/AC自动机+矩阵求逆)

    题目大意:给你$N$个长度相等且互不相同的模式串,现在有一个字符串生成器会不断生成字符,其中每个字符出现的概率是$p_{i}/q_{i}$,当生成器生成的字符串包含了某个模式串,则拥有该模式串的玩家胜 ...

  3. hihoCoder#1036 Trie图

    原题地址 看了这篇博文,总算是把Trie图弄明白了 Runtime Error了无数次,一直不知道为什么,于是写了个脚本生成了一组大数据,发现果然段错误了. 调试了一下午,总算闹明白了,为什么呢? 1 ...

  4. BZOJ 3530 [SDOI2014]数数 (Trie图/AC自动机+数位DP)

    题目大意:略 裸的AC自动机+数位DP吧... 定义f[i][x][0/1]表示已经匹配到了第i位,当前位置是x,0表示没到上限,1到上限,此时数是数量 然而会出现虚拟前导零,即前几位没有数字的情况, ...

  5. 关于Trie KMP AC自动机

    个人认为trie,KMP,AC自动机是思想非常明确的,AC自动机的性质是与KMP算法的思想类似的(失配后跳转) 而KMP是线性的,AC自动机是在tire树上跑KMP,为方便那些不会用指针的小朋友(我也 ...

  6. 2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机)

    2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机) https://www.luogu.com.cn/problem/P2292 题意: 标点符号的出现晚于文字的出 ...

  7. 【hihoCoder】1036 Trie图

    题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...

  8. hihoCoder week4 Trie图

    ac自动机 题目链接 https://hihocoder.com/contest/hiho4/problem/1 参考:https://blog.csdn.net/baidu_30541191/art ...

  9. CF1110H Modest Substrings AC自动机、DP

    传送门 如果\(r-l\)比较小,可以将所有满足条件的串扔进\(AC\)自动机然后在上面DP,从前往后确定字符串的每一位. 但是\(l,r \leq 10^{800}\)就十分不可行,所以需要优化这个 ...

随机推荐

  1. relative 和 absolute 定位关系

    问题: relative 和 absolute 之间的关系是什么?有什么区别? 那,答案呢? relative  相对定位, 以自己没有设置relative 属性之前的位置来定位,占用没有设置rela ...

  2. 重拾Python(2):如何安装第三方库(Windows)

    使用python进行数据分析或者数据处理时,往往需要使用一些库,而使用库之前必须安装它.Anaconda内置了很多常用的第三方库,可以满足绝大部分需求,比如numpy.pandas.matplotli ...

  3. 【转】利用matlab生成随机数函数

    原文地址:利用matlab生成随机数函数 rand(n):生成0到1之间的n阶随机数方阵  rand(m,n):生成0到1之间的m×n的随机数矩阵 (现成的函数) betarnd:贝塔分布的随机数生成 ...

  4. XML系列之--Linq操作带属性的XML(四)

    关于XML,之前解析过电文收发方面的,就是所谓的带表头.前缀(命名空间)SOAP格式.这次需求是解析一个xml的模板(xls内容),然后填充数据,最后保存.需要时可转换xls.pdf等文件.关于这种带 ...

  5. float 的不确定性

    很多时候,大家都知道,浮点型这个东西,本身存储就是一个不确定的数值,你永远无法知道,它是 0 = 0.00000000000000123 还是 0 = 0.00000000000999这样的东西.也许 ...

  6. chrome disable-web-security 关闭安全策略 解决跨域

    Chrome 跨域访问线上接口 时间:2016-04-21 作者:zhongxia 前后端分离之后,联调的时候就会出现问题,那就是Ajax跨域问题. 跨域问题的解决方案有很多种比如常规的 后端使用CR ...

  7. JAVA常用知识点总结---集合篇

    一.Collection 与 Collections的区别:1. Collections:java.util.Collections 是一个包装类.它包含有各种有关集合操作的静态多态方法.此类不能实例 ...

  8. Sorting Slides(二分图匹配——确定唯一匹配边)

    题目描述: Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he is not ...

  9. JasperReport报表开发(一)--原理介绍

    1. JasperReport介绍 JasperReport 是一个开源的Java报表引擎,它不像其他的报表工具,例如Crystal报表是基于Java的,没有自己的表达式语法.Jasper Repor ...

  10. Spark Streaming编程指南

    Overview A Quick Example Basic Concepts Linking Initializing StreamingContext Discretized Streams (D ...