[bzoj2938][Poi2000]病毒 AC自动机
2017-07-25 21:31
477 查看
2938: [Poi2000]病毒
Time Limit: 1 Sec Memory Limit: 128 MB[Submit][Status][Discuss]
Description
二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码。如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的。现在委员会已经找出了所有的病毒代码段,试问,是否存在一个无限长的安全的二进制代码。示例:
例如如果{011, 11, 00000}为病毒代码段,那么一个可能的无限长安全代码就是010101…。如果{01, 11, 000000}为病毒代码段,那么就不存在一个无限长的安全代码。
任务:
请写一个程序:
l 读入病毒代码;
l 判断是否存在一个无限长的安全代码;
l 将结果输出
Input
第一行包括一个整数n,表示病毒代码段的数目。以下的n行每一行都包括一个非空的01字符串——就是一个病毒代码段。所有病毒代码段的总长度不超过30000。
Output
你应在在文本文件WIN.OUT的第一行输出一个单词:l TAK——假如存在这样的代码;
l NIE——如果不存在。
Sample Input
301
11
00000
Sample Output
NIEHINT
补全trie图,找是否存在不经过结尾节点的环#include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; const int N = 30000 + 5; int ch [2],fail ,n,sz; char s ; bool is_end ,vis ; queue<int> q; int cnt,last ,in ; struct Edge{ int to,next; }e[N*2]; void insert(){ int len = strlen(s), now = 0; for( int i = 0; i < len; i++ ){ int x = s[i] - '0'; if( !ch[now][x] ) ch[now][x] = ++sz; now = ch[now][x]; } is_end[now] = true; return; } void make_fail(){ while( !q.empty() ) q.pop(); for( int i = 0; i <= 1; i++ ) if( ch[0][i] ) q.push(ch[0][i]); while( !q.empty() ){ int now = q.front(); q.pop(); for( int i = 0; i <= 1; i++ ){ if( !ch[now][i] ){ ch[now][i] = ch[fail[now]][i]; continue; } fail[ch[now][i]] = ch[fail[now]][i]; if( is_end[fail[ch[now][i]]] ) is_end[ch[now][i]] = true; q.push(ch[now][i]); } } } void insert1( int u, int v ){ e[++cnt].to = v; e[cnt].next = last[u]; last[u] = cnt;} void dfs( int x ){ vis[x] = 1; for( int i = 0; i <= 1; i++ ){ insert1( x, ch[x][i] ); in[ch[x][i]]++; if( !vis[ch[x][i]] && !is_end[ch[x][i]] ) dfs(ch[x][i]); } } bool top(){ int tot = 0; while( !q.empty() ) q.pop(); for( int i = 0; i <= sz; i++ ) if( !in[i] ) q.push(i); while( !q.empty() ){ int now = q.front(); q.pop(); tot++; for( int i = last[now]; i; i = e[i].next ){ in[e[i].to]--; if( !in[e[i].to] ) q.push(e[i].to); } } return tot < sz; } int main(){ scanf("%d", &n); for( int i = 1; i <= n; i++ ){ scanf("%s", s); insert(); } make_fail(); dfs(0); puts(top()?"TAK":"NIE"); return 0; }
相关文章推荐
- bzoj2938 [Poi2000]病毒 ac自动机
- AC自动机【poi2000】bzoj2938 病毒
- 【bzoj2938】[Poi2000]病毒 AC自动机
- BZOJ 2938 [Poi2000]病毒(AC自动机)
- 【bzoj2938】[Poi2000]病毒 AC自动机
- BZOJ2938 POI2000 病毒 补全AC自动机 Trie图判环
- BZOJ_2938_[Poi2000]病毒_AC自动机
- [bzoj2938][Poi2000]病毒_AC自动机
- BZOJ 2938 Poi2000 病毒 AC自动机+拓扑排序
- BZOJ 2938 [Poi2000]病毒
- bzoj2938 [POI2000]病毒(ACAM+拓扑序判环)
- 【bzoj2938】[Poi2000]病毒
- BZOJ 2938: [Poi2000]病毒 AC自动机
- [BZOJ2938][POI2000]病毒 做题笔记
- bzoj 2938 病毒 AC自动机
- [BZOJ2938][POI2000]病毒(AC自动机+dfs)
- 【BZOJ2938】病毒,AC自动机练习
- [BZOJ 2938][Poi2000]病毒
- 【BZOJ2938】病毒(POI2000)-AC自动机+DFS判环
- BZOJ 2938 [Poi2000]病毒