ac自动机

题目链接 https://hihocoder.com/contest/hiho4/problem/1

参考:https://blog.csdn.net/baidu_30541191/article/details/47447175#

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
using namespace std; struct node
{
node *nxt[], *fail;
int ok;
node() {
ok = ;
for(int i=; i<; i++) nxt[i]=NULL;
fail=NULL;
}
}; const int N = 1e6 + ;
char s[N];
node *root; void ins(char *s, int len)
{
node *rt = root;
for(int i=; i<len; i++) {
int x= s[i]-'a';
if(rt->nxt[x] == NULL)
rt->nxt[x] = new node();
rt= rt->nxt[x];
}
rt->ok = ;
return ;
} void build()
{
node *rt = root;
rt->fail = NULL; queue<node*> que;
que.push(rt); while(!que.empty()) {
node *tmp = que.front();
que.pop();
for(int i=; i<; i++) {
if(tmp->nxt[i] != NULL) {
if(tmp == root)
tmp->nxt[i]->fail = root;
else {
node *p = tmp->fail;
while(p!=NULL) {
if(p->nxt[i] != NULL) {
tmp->nxt[i]->fail = p->nxt[i];
break;
}
p = p->fail;
}
if(p==NULL) tmp->nxt[i]->fail = root;
}
que.push(tmp->nxt[i]);
}
}
}
} bool query(char *s, int len)
{
node *rt = root;
int cnt=;
for(int i=; i<len; i++) {
int x = s[i]-'a';
while(!rt->nxt[x] && rt->fail != NULL)
rt=rt->fail;
rt=rt->nxt[x];
if(!rt) rt = root;
node * tmp = rt;
while(tmp->ok==) {
cnt++;
tmp->ok = ;
tmp = tmp->fail;
}
}
if(cnt > )
return true;
return false;
} int main()
{
root = new node();
int n; scanf("%d",&n);
for(int i=; i<n; i++) {
scanf("%s", s), ins(s, strlen(s));
} build();
scanf("%s", s);
bool ok = query(s,strlen(s));
if(ok)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return ;
}

hihoCoder week4 Trie图的更多相关文章

  1. hihoCoder#1036 Trie图

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

  2. hihoCoder 1036 Trie图 AC自动机

    题意:给定n个模式串和一个文本串,判断文本中是否存在模式串. 思路:套模板即可. AC代码 #include <cstdio> #include <cmath> #includ ...

  3. 【hihoCoder】1036 Trie图

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

  4. 【hihoCoder 1036】Trie图

    看了一下简单的$Trie图$,调模板调啊调一连调了$2h$,最后发现$-'a'$打成$-'A'$了hhh,有种摔键盘的冲动. $Trie图$是$Trie树$上建立“前缀边”,不用再像在$Trie树$上 ...

  5. HihoCoder第四周:Trie图

    第四周的题目是前两周的综合,综合在一个是KMP算法的思想,一个是树的这么一个数据结构. 题目 : Trie图 输入 每个输入文件有且仅有一组测试数据. 每个测试数据的第一行为一个整数N,表示河蟹词典的 ...

  6. Trie 图

    时间限制:20000ms 单点时限:1000ms 内存限制:512MB 描述 前情回顾 上回说到,小Hi和小Ho接受到了河蟹先生伟大而光荣的任务:河蟹先生将要给与他们一篇从互联网上收集来的文章,和一本 ...

  7. hiho一下 第二周&第四周:从Trie树到Trie图

    hihocoder #1014 题目地址:http://hihocoder.com/problemset/problem/1014 hihocoder #1036 题目地址: http://hihoc ...

  8. 【AC自动机&&Trie图】积累

    以前KMP和后缀系列(主要是后缀数组,后缀自动机),都刷了一定数量的题,但是对于AC自动机,却有些冷落,罪过. 但是我感觉,在蓝桥杯比赛中AC自动机出现的概率比后缀系列大,简单的会考匹配,稍难一点会考 ...

  9. 小菜鸟 菜谈 KMP->字典树->AC自动机->trie 图 (改进与不改进)

    本文的主要宗旨是总结自己看了大佬们对AC自动机和trie 图 的一些理解与看法.(前沿:本人水平有限,总结有误,希望大佬们可以指出) KMP分割线--------------------------- ...

随机推荐

  1. Python -- print(dataframe)时,省略部分列。

    import pandas as pd # 导入后加入以下列,再显示时显示完全. pd.set_option('display.max_rows',500) pd.set_option('displa ...

  2. Gardener Bo (树剖 + 问题分解)

    一开始没看懂计算答案的第四部和update2,很迷.然后一直推敲之后才发下我计算的时候漏掉一个关键点.没有把加值的影响放到父节点上. #include<bits/stdc++.h> #de ...

  3. 取n到m行

    取n到m行 . select top m * from tablename where id not in (select top n id from tablename order by id as ...

  4. Lua 服务器与客户端实例

    =============================================================== 服务器,main.lua ======================= ...

  5. 机器学习评价方法 - Recall & Precision

    刚开始看这方面论文的时候对于各种评价方法特别困惑,还总是记混,不完全统计下,备忘. 关于召回率和精确率,假设二分类问题,正样本为x,负样本为o: 准确率存在的问题是当正负样本数量不均衡的时候: 精心设 ...

  6. AttributeError: module 'DBBase' has no attribute 'DBBase'

    AttributeError: module 'DBBase' has no attribute 'DBBase' pycharm不会将当前文件目录自动加入自己的sourse_path.右键make_ ...

  7. max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

    elasticsearch启动时遇到的错误 问题翻译过来就是:elasticsearch用户拥有的内存权限太小,至少需要262144: 解决: 切换到root用户 执行命令: sysctl -w vm ...

  8. AtCoder Beginner Contest 070 ABCD题

    题目链接:http://abc070.contest.atcoder.jp/assignments A - Palindromic Number Time limit : 2sec / Memory ...

  9. mysql01

    CREATE TABLE `student`( `studentNo` INT (4) NOT NULL PRIMARY KEY COMMENT '学号', `loginPwd` VARCHAR(20 ...

  10. Modbus库开发笔记之一:实现功能的基本设计(转)

    源: Modbus库开发笔记之一:实现功能的基本设计