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的更多相关文章
随机推荐
- 二叉排序树(Binary Sort Tree)
参考文章:http://blog.csdn.net/ns_code/article/details/19823463 不过博主的使用第一种方法操作后的树已经不是二叉排序树了,值得深思!! #inclu ...
- inline-block元素overflow:hidden对齐问题
inline-block元素设置overflow:hidden后,其本身会上移 解决方法:在该元素或其父元素上设置vertical-align:bottom 原因解释:inline-block元素被设 ...
- 普通浏览器GET请求与Ajax的GET请求的区别
看图 普通浏览器请求 Ajax请求
- java 使用BeanUtils.copyProperties(Object source,Object target) 复制字段值
BeanUtils.copyProperties(person, wsPerson);把person的字段值,复制给wsPerson // 只复制两个实体中,字段名称一样的 很有用的一个功能...
- jsf简介
JSF实现了基于web的以下三个梦想 1.java程序员不必顾虑HTTP的细节,可以按照原本熟悉的事件驱动模型来设计后台系统,并通过一个能担保数据类型无误的数据传递接口将后台系统与前台界面结合在一起. ...
- [转]Android性能优化典范
2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...
- Express开发实例(1) —— Hello,world!
Express是NodeJs开发中最常用的基础模块.NodeJs本身有Http模块,但是易用性并不好,因此有人在此基础上开发了Express模块. 什么是express express提供了丰富的路由 ...
- VS2010创建动态链接库并且使用动态链接库DLL
1.编写动态链接库文件 dll和lib文件 例子: 在新建VS工程时选择DLL 空项目 ----------hello.h-------- #include <stdio.h> #prag ...
- DAY5 php + mysql 写一个简单的sql注入平台
php mysql 在浏览器输入用户名,去数据库查询.查到则显示在浏览器,查不到则显示空. sql 里面三个字段 id username password create table t1 (id in ...
- jsp+servlet