http://www.lydsy.com/JudgeOnline/problem.php?id=2938

https://www.luogu.org/problemnew/show/P2444

二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码。如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的。现在委员会已经找出了所有的病毒代码段,试问,是否存在一个无限长的安全的二进制代码。

对于多串,先建立AC自动机。

然后考虑对于没有病毒代码的代码来说,它会在AC自动机上一直走直到跳出。

而如果走出了一个环的话就意味着它可以沿着这个环无限延伸——这就是我们所要求的。

所以我们建立AC自动机跑dfs搜环(当然危险节点不能走),如果有环就是TAK,否则就是NIE。

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
const int N=;
const int M=;
struct trie{
bool ed;
int a[],fail;
}tr[M*];
int cnt=;
char s[M];
inline void insert(){
int now=;
int len=strlen(s);
for(int i=;i<len;i++){
int t=s[i]-'';
if(!tr[now].a[t]){
cnt++;
tr[now].a[t]=cnt;
}
now=tr[now].a[t];
}
tr[now].ed=;
return;
}
void getfail(){
queue<int>q;
tr[].fail=;
for(int i=;i<;i++){
if(tr[].a[i]){
tr[tr[].a[i]].fail=;
q.push(tr[].a[i]);
}
}
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=;i<;i++){
if(tr[u].a[i]){
int v=tr[u].a[i];
tr[v].fail=tr[tr[u].fail].a[i];
tr[v].ed|=tr[tr[v].fail].ed;
q.push(tr[u].a[i]);
}else{
tr[u].a[i]=tr[tr[u].fail].a[i];
}
}
}
return;
}
bool vis[M*],met[M*];
bool dfs(int u){
vis[u]=;
for(int i=;i<;i++){
int v=tr[u].a[i];
if(vis[v])return ;
if(tr[v].ed||met[v])continue;
met[v]=;
if(dfs(v))return ;
}
vis[u]=;
return ;
}
int main(){
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>s;
insert();
}
getfail();
if(dfs())puts("TAK");
else puts("NIE");
return ;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者:luyouqi233。               +

+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++

最新文章

  1. GNU Radio Radar Toolbox
  2. 『TCP/IP详解——卷一:协议』读书笔记——17
  3. broadcom移植到openwrt总结
  4. Windows Phone 8.1 Update1 支持中文“小娜”及开发者模拟器更新
  5. wordpress后台打开慢/卡顿的解决方法
  6. [转载]mvc使用JsonResult返回Json数据
  7. hduTHE MATRIX PROBLEM(差分约束)
  8. 一个人的旅行--hdu2066
  9. 怎么给没链接的flash加超链接
  10. OA项目设计的能力③
  11. 介绍Office 365 中文用户社区 4.0
  12. wampserver启动不起来的原因?
  13. CSS(一)解析浮动塌陷与清除浮动
  14. 斐波那契数列 (C#)
  15. SQL瓶颈分析,以及适应最佳执行计划的探讨
  16. tree与GridView交互
  17. bzoj 4011
  18. python测试开发django-56.模板渲染markdown语法+代码高亮
  19. Python之ftp服务器
  20. Python中*args和**kwargs 的简单使用

热门文章

  1. yarn 原理
  2. 问题:MongoDB C# driver异常:Truncation resulted in data loss
  3. android学习十 ActionBar
  4. 「日常训练」ZgukistringZ(Codeforces Round #307 Div. 2 B)
  5. hdu5305 Friends(dfs,多校题)
  6. Visual Studio Code——PHP Debug扩展
  7. 使用深度学习来破解 captcha 验证码(转)
  8. 特殊符号 &amp; 以太坊
  9. HADOOP docker(十):hdfs 结构体系
  10. [leetcode-667-Beautiful Arrangement II]