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. c++ 常用的数据结构

    set // constructing sets #include <iostream> #include <set> void checkin(std::set<int ...

  2. Window对象属性

    2018-11-28 12:21:20

  3. Python - 3. Input and Output

    from:http://interactivepython.org/courselib/static/pythonds/Introduction/InputandOutput.html Input a ...

  4. js三目学习

    <script> var n=1; n>1?document.write('大于1哦'):document.write('小于或等于1哦') //n=n>1?document. ...

  5. 20165305 苏振龙《Java程序设计》第五周学习总结

    第七章 Java支持在一个类中声明另一个类,这样的类称作内部类,而包含内部类的类成为内部类的外嵌类. 和某类有关的匿名类就是该类的一个子类,该子类没有明显的用类声明来定义,所以称做匿名类. 和某接口有 ...

  6. Linux基础命令---文本格式转换fmt

    fmt 将指定文件的内容,按照指定的格式重新排版,结果送到标准输出. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法     ...

  7. activiti 报 next dbid

    记录一下吧. 今天将生产环境的几个服务节点改成集群模式,其中包含activiti审批服务节点,其中各个服务几点间数据通信采用MQ(与本文无关). 然后报出如题错误. 究其原因就是,在启动activit ...

  8. navicat 连接postgresql报错pg_hba.conf

    PostgreSQ数据库为了安全,它不会监听除本地以外的所有连接请求,当用户通过JDBC访问是,会报一些如下的异常: org.postgresql.util.PSQLException: FATAL: ...

  9. Array和ArrayList不同

    Employee[] array = new Employee[10]; ArrayList<Employee> staff = new ArrayList<>(); 不同 A ...

  10. 纯Java增删改查

    //自己写的一个完整的带增删改查提交重置功能的表单代码.package com.l16.test5;import java.awt.Color;import java.awt.Container;im ...