hihoCoder week4 Trie图
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图的更多相关文章
- hihoCoder#1036 Trie图
原题地址 看了这篇博文,总算是把Trie图弄明白了 Runtime Error了无数次,一直不知道为什么,于是写了个脚本生成了一组大数据,发现果然段错误了. 调试了一下午,总算闹明白了,为什么呢? 1 ...
- hihoCoder 1036 Trie图 AC自动机
题意:给定n个模式串和一个文本串,判断文本中是否存在模式串. 思路:套模板即可. AC代码 #include <cstdio> #include <cmath> #includ ...
- 【hihoCoder】1036 Trie图
题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...
- 【hihoCoder 1036】Trie图
看了一下简单的$Trie图$,调模板调啊调一连调了$2h$,最后发现$-'a'$打成$-'A'$了hhh,有种摔键盘的冲动. $Trie图$是$Trie树$上建立“前缀边”,不用再像在$Trie树$上 ...
- HihoCoder第四周:Trie图
第四周的题目是前两周的综合,综合在一个是KMP算法的思想,一个是树的这么一个数据结构. 题目 : Trie图 输入 每个输入文件有且仅有一组测试数据. 每个测试数据的第一行为一个整数N,表示河蟹词典的 ...
- Trie 图
时间限制:20000ms 单点时限:1000ms 内存限制:512MB 描述 前情回顾 上回说到,小Hi和小Ho接受到了河蟹先生伟大而光荣的任务:河蟹先生将要给与他们一篇从互联网上收集来的文章,和一本 ...
- hiho一下 第二周&第四周:从Trie树到Trie图
hihocoder #1014 题目地址:http://hihocoder.com/problemset/problem/1014 hihocoder #1036 题目地址: http://hihoc ...
- 【AC自动机&&Trie图】积累
以前KMP和后缀系列(主要是后缀数组,后缀自动机),都刷了一定数量的题,但是对于AC自动机,却有些冷落,罪过. 但是我感觉,在蓝桥杯比赛中AC自动机出现的概率比后缀系列大,简单的会考匹配,稍难一点会考 ...
- 小菜鸟 菜谈 KMP->字典树->AC自动机->trie 图 (改进与不改进)
本文的主要宗旨是总结自己看了大佬们对AC自动机和trie 图 的一些理解与看法.(前沿:本人水平有限,总结有误,希望大佬们可以指出) KMP分割线--------------------------- ...
随机推荐
- django 设置不带后缀的访问路径
在urls.py 设置空路径,并指向对应的html文件 url(r'^$', views.index),
- 【转】python3实现自动化框架robotframework
由于python2只更新到2020年,python3是未来的主流,为了适应技术的变化python3实现robotframework是迟早的事 1.下载最新版本的python3.7,可根据自己电脑的位数 ...
- 对象的copy
一般来讲,java中,对象的copy一般是通过Beans.copy(B, A);用来将A对象复制给B对象,包括对象里面的属性和值. 但但但...... 这样做,有一个很严重的问题,那就是:如果把A对象 ...
- python pymssql 连接数据库
1)写在前面 远程连接数据库的时候,端口前面都是用的逗号, 因为惯性思维, 就傻傻的把 ip+,+端口 赋值给server了,然后一直报错, pymssql.InterfaceError: Co ...
- Eclipse创建maven工程后没有build path解决方案
1.修改maven工程下的.project文件为如下内容 <?xml version="1.0" encoding="UTF-8"?> <pr ...
- Linux环境 vi/vim ESC无法退出原因
原因是输入模式是中文的,需要切换成英文半角符号输入命令!
- Linux基础命令---ping
ping ping指令可以发送ICMP请求到目标地址,如果网络功能正常,目标主机会给出回应信息.ping使用ICMP协议强制发送ECHO_REQUEST报文到目标主机,从主机或网关获取ICMP ECH ...
- golang学习笔记19 用Golang实现以太坊代币转账
golang学习笔记19 用Golang实现以太坊代币转账 在以太坊区块链中,我们称代币为Token,是以太坊区块链中每个人都可以任意发行的数字资产.并且它必须是遵循erc20标准的,至于erc20标 ...
- 安装使用zookeeper
1,加压 2,复制zoo_sample.cfg命名为zoo.cfg 3,在conf同级目录下新建一文件夹 data 4,修改数据存放目录 5,启动zookeeper
- to refactor for refactor
v1.1 if all algorithm are in one function, it will expand. so each operate type should be separated. ...