http://begin.lydsy.com/JudgeOnline/problem.php?id=2774(poi病毒)
来源:互联网 发布:拟线性偏好知乎 编辑:程序博客网 时间:2024/05/01 05:50
2774: Poi2000 病毒
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 5 Solved: 4
[Submit][Status][Web Board]
Description
二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码。
如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的。现在委员会已经找出了所有的病毒代码段
试问,是否存在一个无限长的安全的二进制代码。
示例:
例如如果{011, 11, 00000}为病毒代码段,那么一个可能的无限长安全代码就是010101…。如果{01, 11, 000000}为病毒代码段,那么就不存在一个无限长的安全代码。
任务:
请写一个程序:
读入病毒代码,判断是否存在一个无限长的安全代码;
如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的。现在委员会已经找出了所有的病毒代码段
试问,是否存在一个无限长的安全的二进制代码。
示例:
例如如果{011, 11, 00000}为病毒代码段,那么一个可能的无限长安全代码就是010101…。如果{01, 11, 000000}为病毒代码段,那么就不存在一个无限长的安全代码。
任务:
请写一个程序:
读入病毒代码,判断是否存在一个无限长的安全代码;
Input
第一行包括一个整数n,表示病毒代码段的数目。
以下的n行每一行都包括一个非空的01字符串——就是一个病毒代码段。所有病毒代码段的总长度不超过30000。
以下的n行每一行都包括一个非空的01字符串——就是一个病毒代码段。所有病毒代码段的总长度不超过30000。
Output
输出一个单词:
TAK——假如存在这样的代码;
NIE——如果不存在。
TAK——假如存在这样的代码;
NIE——如果不存在。
Sample Input
3011100000
Sample Output
NIE
HINT
Source
题解:
首先我们把所有串建一个AC自动机
方便起见我们直接把fail指针合并到子结点
如果一个串能无限长,也就是说它可以在AC自动机上一直进行匹配但就是匹配不上
也就是说匹配指针不能走到val为1的结点,设这个点为x
即root..x是一个病毒串
那么fail指针指向x的y也不能走
因为root..x是root..y的一个后缀
处理出来判断有向图是否有环
dfs即可
#include<iostream>#include<cstring>#include<cstdio>#define maxn 30001using namespace std;int n,tot;char s[maxn];int sum[maxn],son[maxn][2],lis[maxn],fai[maxn];int ins[maxn],vis[maxn];void insert(){ int p=0; scanf("%s",s+1); for (int i=1; s[i]; p=son[p][s[i]-'0'],i++) if (!son[p][s[i]-'0']) son[p][s[i]-'0']=++tot; sum[p]=1;}void failed(){ int head=0,tail=1; lis[1]=0; fai[0]=-1; while (head<tail) { int x=lis[++head]; for (int i=0; i<=1; i++) { int v=son[x][i]; if (!v) { son[x][i]=son[fai[x]][i]; continue; } lis[++tail]=v; int p=fai[x]; while (p!=-1 && !son[p][i]) p=fai[p]; if (p==-1) fai[v]=0; else fai[v]=son[p][i],sum[v]|=sum[son[p][i]]; } }}bool dfs(int x){ ins[x]=1; for (int i=0; i<2; i++) { int v=son[x][i]; if (ins[v]) return 1; if (vis[v] || sum[v]) continue; vis[v]=1; if (dfs(v)) return 1; } ins[x]=0; return 0;}int main(){ cin>>n; tot=0; memset(son,0,sizeof(son)); for (int i=1; i<=n; i++) insert(); failed(); if (dfs(0)==1) cout<<"TAK"<<endl; else cout<<"NIE"<<endl;}
0 0
- http://begin.lydsy.com/JudgeOnline/problem.php?id=2774(poi病毒)
- http://begin.lydsy.com/JudgeOnline/problem.php?id=2770(PKU2503 Babelfish)
- Zju1290 Word-Search Wonder(http://begin.lydsy.com/JudgeOnline/problem.php?id=2768)
- http://211.67.33.62/JudgeOnline/problem.php?id=1014,排序二叉树
- http://acm.pku.edu.cn/JudgeOnline/problem?id=1284 (原根)
- http://acm.nyist.net/JudgeOnline/problem.php?pid=290
- http://acm.nyist.net/JudgeOnline/problem.php?pid=434
- http://acm.nyist.net/JudgeOnline/problem.php?pid=13
- http://acm.nyist.net/JudgeOnline/problem.php?pid=239
- http://acm.nyist.net/JudgeOnline/problem.php?pid=129
- http://acm.nyist.net/JudgeOnline/problem.php?pid=431
- http://acm.nyist.net/JudgeOnline/problem.php?pid=301
- http://acm.nyist.net/JudgeOnline/problem.php?pid=451
- http://acm.nyist.net/JudgeOnline/problem.php?pid=459
- http://acm.nyist.net/JudgeOnline/problem.php?pid=20
- http://acm.nyist.net/JudgeOnline/problem.php?pid=267
- http://acm.nyist.net/JudgeOnline/problem.php?pid=305
- http://acm.nyist.net/JudgeOnline/problem.php?pid=309
- Android 优秀文章收集整理集合
- 5个最佳的Android测试框架(带示例)
- http://begin.lydsy.com/JudgeOnline/problem.php?id=2770(PKU2503 Babelfish)
- 10天精通Sass 之 Sass运算
- jsp内置对象之request
- http://begin.lydsy.com/JudgeOnline/problem.php?id=2774(poi病毒)
- [HNOI2004]Language L语言
- [Usaco2008 Dec]Secret Message 秘密信息
- [TJOI2013]单词
- [NOI2011]阿狸的打字机(好题!!!!)
- 【UVa】10200 - Prime Time(打表)
- 2781: [JSOI2007]文本生成器
- 关于UPnP Device Architecture 2.0的阅读笔记(十二)
- 2786: [JSOI]Word Query电子字典