【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=2938

【题目大意】

  给出一些病毒串,问是否存在不包含任何病毒串的无限长的字符串

【题解】

  首先我们对病毒串建立AC自动机,如果我们能够在AC自动机上无限跑但是不成功匹配,
  说明就存在这样的安全串,我们在AC自动机上做搜索,
  若存在不经过match的环,那就是TAK,否则就是NIE。

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=30010;
namespace AC_DFA{
const int Csize=2;
int tot,son[N][Csize],sum[N],fail[N],q[N],match[N];
void Initialize(){
memset(match,0,sizeof(int)*(tot+1));
memset(fail,0,sizeof(int)*(tot+1));
for(int i=0;i<=tot;i++)for(int j=0;j<Csize;j++)son[i][j]=0;
tot=0; fail[0]=-1;
}
inline int Tr(char ch){return ch-'0';}
int Insert(char *s){
int x=0;
for(int l=strlen(s),i=0,w;i<l;i++){
if(!son[x][w=Tr(s[i])]){
son[x][w]=++tot;
}x=son[x][w];
}sum[x]++;
return x;
}
void MakeFail(){
int h=1,t=0,i,j,x=0;
for(i=0;i<Csize;i++)if(son[0][i])q[++t]=son[0][i];
while(h<=t)for(x=q[h++],i=0;i<Csize;i++)
if(son[x][i]){
fail[son[x][i]]=son[fail[x]][i],q[++t]=son[x][i];
match[son[x][i]]=sum[son[x][i]]?son[x][i]:match[fail[son[x][i]]];
}else son[x][i]=son[fail[x]][i];
}
}
using namespace AC_DFA;
int inq[N],vis[N];
bool dfs(int x){
inq[x]=1;
for(int i=0;i<Csize;i++){
int y=son[x][i];
if(inq[y])return 1;
if(vis[y]||match[y])continue;
vis[y]=1;
if(dfs(y))return 1;
}inq[x]=0;
return 0;
}
int n;
char s[N];
int main(){
Initialize();
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s);
Insert(s);
}MakeFail();
if(dfs(0))puts("TAK");
else puts("NIE");
return 0;
}

最新文章

  1. SQL Server-聚焦在视图和UDF中使用SCHEMABINDING(二十六)
  2. Struts2框架深入详解版
  3. MongoDB环境准备
  4. FreeBSD打开DTrace支持
  5. 在eclipse中运行perl代码,需要配置的插件以及方法
  6. STM
  7. C++开发必看 四种强制类型转换的总结 [转]
  8. 如何在JavaScript里防止事件函数的高频触发和调用
  9. Android之HTTP网络通信--GET传递
  10. [Quick-x]移动CCEditbox的父对象导致输入框位置偏移问题
  11. zTree判断是否为父节点
  12. EJB开发第一个无状态会话bean、开发EJBclient
  13. ExtJs4 笔记(10) Ext.tab.Panel 选项卡
  14. 【割点】【割边】tarjan
  15. DVWA 黑客攻防演练(四)文件包含 File Inclusion
  16. vim代码格式化插件clang-format
  17. Git图形化界面客户端
  18. 【洛谷p1403 】【AHOI2005】约数研究
  19. CRM函数CRM_ORDER_MAINTAIN封装
  20. 记录常用的git命令

热门文章

  1. php常用函数——数组函数
  2. VideoJS 与 Framework7 中 fastclick 冲突问题
  3. hibernate CasCade deleted object ould be re-saved by cascade
  4. 64_p9
  5. HttpURLConnection传json
  6. RF和adaboost
  7. java基础2 判断语句:if ... else 语句和 switch 语句
  8. Morris Traversal方法遍历
  9. 记一次测试环境下PXC集群问题《经验总结》
  10. 关于真多核和加多核&amp;线程由哪几部分组成