hnu10104
AC自动机+DFS
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std; #define D(x) const int MAX_LEN = * (1e4) + ;
const int MAX_NODE_NUM = MAX_LEN;
const int MAX_CHILD_NUM = ; struct Trie
{
int next[MAX_NODE_NUM][MAX_CHILD_NUM];
int fail[MAX_NODE_NUM];
int node_cnt;
bool vis[MAX_NODE_NUM]; //set it to false
bool virus[MAX_NODE_NUM];
int root; void init()
{
node_cnt = ;
root = newnode();
memset(vis, , sizeof(vis));
memset(virus, , sizeof(virus));
} int newnode()
{
for (int i = ; i < MAX_CHILD_NUM; i++)
next[node_cnt][i] = -;
virus[node_cnt++] = false;
return node_cnt - ;
} int get_id(char a)
{
return a - '';
} void insert(char buf[])
{
int now = root;
for (int i = ; buf[i]; i++)
{
int id = get_id(buf[i]);
if (next[now][id] == -)
next[now][id] = newnode();
now = next[now][id];
}
virus[now] = true;
D(printf("virus[%d] = %d\n", now, virus[now]));
} void build()
{
queue<int>Q;
fail[root] = root;
for (int i = ; i < MAX_CHILD_NUM; i++)
if (next[root][i] == -)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while (!Q.empty())
{
int now = Q.front();
Q.pop();
for (int i = ; i < MAX_CHILD_NUM; i++)
if (next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
if (virus[fail[next[now][i]]])
virus[next[now][i]] = true;
Q.push(next[now][i]);
}
}
} bool dfs(int u)
{
D(printf("%d\n", u));
vis[u] = true;
for (int i = ; i < MAX_CHILD_NUM; i++)
{
int v = next[u][i];
D(printf(" %d\n", v));
if (virus[v])
continue;
if (vis[v])
return true;
if (dfs(v))
return true;
}
vis[u] = false;
return false;
} void debug()
{
for(int i = ;i < node_cnt;i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],virus[i]);
for(int j = ;j < MAX_CHILD_NUM;j++)
printf("%2d",next[i][j]);
printf("]\n");
}
}
}ac; char st[MAX_LEN];
int n; int main()
{
scanf("%d", &n);
ac.init();
for (int i = ; i < n; i++)
{
scanf("%s", st);
ac.insert(st);
}
ac.build();
memset(ac.vis, , sizeof(ac.vis));
if (ac.dfs(ac.root))
puts("TAK");
else
puts("NIE");
return ;
}
hnu10104的更多相关文章
随机推荐
- 深入Spring IOC源码之ResourceLoader
在<深入Spring IOC源码之Resource>中已经详细介绍了Spring中Resource的抽象,Resource接口有很多实现类,我们当然可以使用各自的构造函数创建符合需求的Re ...
- [asp.net core]project.json(1)
摘要 前面介绍了使用vs2015新建asp.net core web的内容,这篇文章学习下project.json文件的内容. project.json 原文:https://docs.microso ...
- SQL笔记 - CTE递归实例(续):显示指定部门的全称
前一篇文章中已经可以取得所有部门的全称,但现在又有个新的需求: 只想得到某一个部门的部门全称,虽然可以用where条件来过滤,但是会有点小浪费. 这时我们可以从后往前找,先看下效果: 最后一条就是,行 ...
- oracle中时间运算
Oracle两个函数相减,默认得到的是天数,按日期格式,精准到响应的精度,如用sysdate(2015/12/7 10:17:52),时间差精确到秒. 在此基础上,oracle两个时间相减默认天数*2 ...
- go语言mongdb管道使用
原始json: { "listsn": "", "code": "fwq_add", "detail" ...
- HDU 2007
/*杭电ACM ID:2007*/ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() { int in1, in2 ...
- Maven工程中报 Missing artifact jdk.tools:jdk.tools:
jdk.tools:jdk.tools是与JDK一起分发的一个JAR文件,可以如下方式加入到Maven项目中:<dependency> <groupId>jdk.tool ...
- jQuery - 动态创建iframe并加载页面
<html> <head> <script language="JavaScript" src="jquery-1.11.1.min.js& ...
- 教你如何删除tomcat服务器的stdout.log文件
用Tomcat做WEB服务器的人都知道,有个很让人头痛的问题,就是stdout.log日志文件会自动增长,而且增长得很快. 先来看看我的痛处吧,公司有个WEB应用,就是用Tomcat作为服务器的,由于 ...
- win7平台下React-Native Android:Unable to upload some APKs
一.问题描述 根据网络上的Win7平台下React-native配置教程配置好开发环境的过程中,在艰难进行到react-native run-android这一步时,发现一直出现错误,截图如下: 错误 ...