题意:给定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. background是什么样式?

    background是什么样式? 给标签添加背景图片 分为: background: url("图片路径");    #添加图片 background-position: xpx ...

  2. Windows核心编程&内存管理

    1. 每个进程都有自己的虚拟地址空间,对于32位机器而言,这个地址空间的大小为4GB(2^32 / 1024^3),这个虚拟地址空间只不过是一个内存地址空间, 为了能够正常读/写数据,我们还需要把物理 ...

  3. 开发步骤Dubbo、spring mvc、springboot、SSM整合开发步骤

    一.Dubbo开发步骤: 链接:https://pan.baidu.com/s/1pMPO1kf 密码:9zaa 第一: 1.创建consumer工程2.在pom.xml文件下添加配置3.添加appl ...

  4. MyEclipse中阿里JAVA代码规范插件(P3C)的安装及使用

    JAVA代码规范插件(P3C)是阿里巴巴2017年10月14日在杭州云栖大会上首发的,使之前的阿里巴巴JAVA开发手册正式以插件形式公开走向业界.插件的相关信息及安装包都可以在GitHub(https ...

  5. js中的监听事件总结

    javascript事件与功能说明大全:http://tools.jb51.net/table/javascript_event 1.滚动条监听事件 例1:监听滚动条距离页面顶端距离 <scri ...

  6. Mysql编译安装及优化

    采取编译安装的方法,其好处为:编译安装与平台无关,安装的MySQL目录独立,维护起来方便,而且拥有更好的性能. 环境:CentOS release 6.9 (Final)  x86_64 1)下载my ...

  7. oracle pl/sql中的循环及if语句

    for循环 /* for循环打印1到10 */ set serveroutput on; declare begin .. loop dbms_output.put_line(i); end loop ...

  8. History对象和location对象

    history对象 History对象包含用户在浏览器窗口中访问过的url.不是所有浏览器都支持该对象. 属性length   返回浏览器历史列表中的URL数量. 方法:back() 加载histor ...

  9. codechef Dynamic GCD [树链剖分 gcd]

    Dynamic GCD 题意:一棵树,字词树链加,树链gcd 根据\(gcd(a,b)=gcd(a,a-b)\) 得到\(gcd(a_1, a_2, ..., a_i) = gcd(a_1, a_1- ...

  10. python中常见的三种句型if,while,for

    1.if语句: 特别说明:条件后面的冒号不能少,同样必须是英文字符. 特别特别说明:if内部的语句需要有一个统一的缩进,一般用4个空格.python用这种方法替代了其他很多编程语言中的{}. num= ...