收获:

  1、AC自动机可以在建立fail时将一些不存在的儿子指针指向对应的位置。

  2、判断环时不要想当然地写个这样的版本:

bool dfs( int u ) {
if( vis[u] ) return true;
vis[u] = true;
for( int t=; t<g[u].size(); t++ ) {
int v=g[u][t];
if( dfs(v) ) return true;
}
vis[u] = false;
return false;
}

它不是O(n)的,然后我就T了,搞了好久才弄好。

  3、存在一个无限长的不与给定pattern匹配的text,那么在该text上跑ac自动机会无限循环,反之一定会在有限的时间内结束。

 /**************************************************************
Problem: 2938
User: idy002
Language: C++
Result: Accepted
Time:40 ms
Memory:2316 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#define maxn 60010
using namespace std; int son[maxn][], fail[maxn], ikey[maxn], ntot;
int vis[maxn], cir[maxn];
char str[maxn]; void insert( const char *P ) {
int n=strlen(P);
int u=;
for( int i=; i<n; i++ ) {
int c=P[i]-'';
if( !son[u][c] ) son[u][c] = ++ntot;
u=son[u][c];
}
ikey[u] = true;
}
void build_fail() {
queue<int> qu;
for( int c=; c<; c++ ) {
int v=son[][c];
if( !v ) continue;
qu.push( v );
fail[v] = ;
}
while( !qu.empty() ) {
int u=qu.front();
qu.pop();
for( int c=; c<; c++ ) {
int v=son[u][c];
int w=fail[u];
if( !v ) {
son[u][c] = son[fail[u]][c];
continue;
}
while( w && !son[w][c] ) w=fail[w];
fail[v] = son[w][c];
ikey[v] |= ikey[fail[v]];
qu.push( v );
}
}
}
void dfs( int u ) {
vis[u] = true;
for( int c=; c<; c++ ) {
int v=son[u][c];
if( cir[v] || ikey[v] ) continue;
if( !vis[v] )
dfs(v);
else {
printf( "TAK\n" );
exit();
}
}
cir[u] = true;
}
int main() {
int n;
scanf( "%d", &n );
for( int i=; i<n; i++ ) {
scanf( "%s", str );
insert( str );
}
build_fail();
dfs( );
printf( "NIE\n" );
}

bzoj 2938的更多相关文章

  1. BZOJ 2938: [Poi2000]病毒 [AC自动机 拓扑排序]

    2938: [Poi2000]病毒 题意:判断是否存在无限长的不含模式串的字符串.只有01. 建出套路DP的转移图,判断有环就行了 练习一下拓扑排序 #include <iostream> ...

  2. BZOJ 2938: [Poi2000]病毒

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 693  Solved: 360[Submit][Status][Di ...

  3. BZOJ 2938 [Poi2000]病毒(AC自动机)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2938 [题目大意] 给出一些病毒串,问是否存在不包含任何病毒串的无限长的字符串 [题解 ...

  4. BZOJ 2938 [POI2000]病毒 (剪枝/A*迭代搜索)

    LOJ BZOJ 题目大意:给你一些模式串,问是否存在一个无限长的文本串,不包含任何一个给定的模式串 并没有想到去模拟合法的文本串在模式串的Trie图上匹配的过程..我好菜啊 如果一个字符串合法,那么 ...

  5. bzoj 2938 AC自动机

    根据题意建出trie图,代表单词的点不能走,直接或间接指向它的点也不能走.这样的话如果能在图中找到一个环的话就是TAK,否则是NIE. #include<iostream> #includ ...

  6. BZOJ.2938.[POI2000]病毒(AC自动机)

    题目链接 \(Description\) 给n个模式串,问是否存在长度无限的主串,使得任何一个模式串都没有在主串中出现. \(Solution\) 先建AC自动机. 假设我们有了一个无限长的安全代码, ...

  7. bzoj 2938 AC自动机 + dfs判环

    #include<bits/stdc++.h> #define LL long long #define ll long long #define fi first #define se ...

  8. 病毒(bzoj 2938)

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

  9. 【BZOJ】【2938】【POI2000】病毒

    AC自动机 好题>_<(其实是一次AC有些感动) 嗯要找到无限长的一个字符串不包含任何一个模板串,就意味着在AC自动机(Trie图)上找到一个不经过任何一个危险结点的环,深搜一下就好了…… ...

随机推荐

  1. 【译】第四篇 SQL Server代理配置数据库邮件

    本篇文章是SQL Server代理系列的第四篇,详细内容请参考原文. 正如这一系列的前几篇所述,SQL Server代理作业是由一系列的作业步骤组成,每个步骤由一个独立的类型去执行.SQL Serve ...

  2. C++之 extern C的作用详解

    extern "C"的主要作用就是为了能够正确实现C++代码调用其他C语言代码.加上extern "C"后,会指示编译器这部分代码按C语言的进行编译,而不是C+ ...

  3. netif_receive_skb->__netif_receive_skb_core

    在设备驱动收包之后,会通过netif_receive_skb将收取的包,按照注册的协议回调,传递到上层进行处理: /* 将skb传递到上层 */ static int __netif_receive_ ...

  4. Android设备相关配置

    http://source.android.com/devices/tech/storage/index.html Android supports devices with external sto ...

  5. 内存不够清理方法,costdown项目时如果裁剪不下来,也可以参考

    Linux下清理内存和Cache方法 /proc/sys/vm/drop_caches 频繁的文件访问会导致系统的Cache使用量大增 $ free -m total used free shared ...

  6. tera term通过ttl脚本 自动连接服务器(转自http://www.cnblogs.com/wxb0328/p/teraterm.html)

    转自http://www.cnblogs.com/wxb0328/p/teraterm.html 在现在的这个公司一直使用tera term来远程连接服务器,感觉很方便,特别是它的ttl脚本配置的自动 ...

  7. Elasticsearch5.0 安装问题集锦【转】

    转自 Elasticsearch5.0 安装问题集锦 - 代码&优雅着&生活 - 博客园http://www.cnblogs.com/sloveling/p/elasticsearch ...

  8. STL中heap相关函数

    heap并不是属于STL中的containers,而是在<algorithm>下提供了相关的函数 make_heap,sort_heap,pop_heap,push_heap 函数的说明: ...

  9. SipDroid +miniSIPServer搭建SIP局域网语音通话(一)

    最近在做语音通讯功能,参考下优秀开源软件SIPDroid好就这个了,svn check下最新的源代码 http://sipdroid.googlecode.com/svn/trunk/sipdroid ...

  10. [How to] MapReduce on HBase ----- 简单二级索引的实现

    1.简介 MapReduce计算框架是二代hadoop的YARN一部分,能够提供大数据量的平行批处理.MR只提供了基本的计算方法,之所以能够使用在不用的数据格式上包括HBase表上是因为特定格式上的数 ...