LG2444/BZOJ2938 「POI2000」病毒 AC自动机
2024-09-05 03:26:58
问题描述
I \(\mathrm{AC}\)自动机
\(\mathrm{AC}\)自动机是一种多模式串匹配算法,本萌新今天刚学了它qwq
约定在构造\(\mathrm{AC}\)自动机的过程中,\(\mathrm{Trie}\)树上的边和由于\(\mathrm{AC}\)自动机中这一句:
else ch[x][i]=ch[fail[x]][i];
所新建的边为实边。
也就是\(x\)到\(ch[x][0]\)和\(ch[x][1]\)的边为实边。
II 危险!
什么样的字符串是有病毒的?包含了某个模式串的文本串。
这样的文本串一定在AC自动机上经过一个字符串的结尾节点。
我们称这样的节点是危险的。
III 解法
想象一下,假如在AC自动机上有一个环,我就可以让这个文本串一直在这个环上跑,转啊转啊转。
就像最短路有了负环一样。
所以,只要在AC自动机上,存在一个环,使得这个环上和从根到这个环的路径上,所有的点都不是危险节点,就有解,否则无解。
IV \(\mathrm{code}\)
#include<bits/stdc++.h>
using namespace std;
template <typename Tp>
void read(Tp &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') {
ch=getchar();fh=-1;
}
else fh=1;
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
x*=fh;
}
int n;
int ch[300000][2],tot;
bool ed[300000];
int fail[300000],root;
string s;
int chk(char s){
return s-'0';
}
void insert(){
int siz=s.size();int p=root;
for(int i=0;i<siz;i++){
int d=chk(s[i]);
if(!ch[p][d]) ch[p][d]=++tot;
p=ch[p][d];
}
ed[p]=1;
}
void pre(){
queue<int>q;
for(int i=0;i<2;i++){
if(ch[root][i]) fail[ch[root][i]]=root,q.push(ch[root][i]);
}
while(!q.empty()){
int x=q.front();q.pop();
for(int i=0;i<2;i++){
if(ch[x][i]){
fail[ch[x][i]]=ch[fail[x]][i],q.push(ch[x][i]);
if(ed[ch[fail[x]][i]]) ed[ch[x][i]]=1;
}
else ch[x][i]=ch[fail[x]][i];
}
}
}
bitset<300000>ins;
void dfs(int x){
if(ins[x]){
puts("TAK");exit(0);
}
ins[x]=1;
for(int i=0;i<2;i++){
if(!ch[x][i]||ed[ch[x][i]]) continue;
dfs(ch[x][i]);
}
ins[x]=0;
}
int main(){
read(n);
for(register int i=1;i<=n;i++){
cin>>s;insert();
}
pre();dfs(root);
puts("NIE");
return 0;
}
最新文章
- openresty 前端开发序
- 如何使用VS在SharePont 2013中插入ashx文件
- Git在window的使用(TortoiseGit)之一
- javascript 对象初探(二)--- 返回对象的函数
- Bootstrap Chart组件使用分享
- T-SQL Apply的用法
- jsp基础知识(基本的语法及原理)
- Java IO(四)
- 下拉刷新控件(1)PullToRefreshList示例
- PHP PSR-2 代码风格规范 (中文版)
- Spring MVC 学习笔记 json格式的输入和输出
- hexo摸爬滚打之进阶教程
- 数据库服务器构建和部署列表(For SQL Server 2012)
- Android Studio 不得不知的20大快捷键
- 前端iFrame跨域问题
- 3-3 Hadoop集群完全分布式配置部署
- 查看jar包的jdk版本
- ZABBIX安装过程中relocation error报错解决办法
- python使用udp实现聊天器
- WireShark Flow capture analysis
热门文章
- pytorch 建立模型的几种方法
- 浅谈vue中的计算属性和侦听属性
- [题解向] CF#Global Round 1の题解(A $\to$ G)
- Regex quick reference
- 一篇文章弄懂flex布局
- 1+x 证书 Web 前端开发 css 专项练习
- python接口自动化12-pytest前后置与fixture
- 分析FAT32内部结构-入门篇-
- 11-scrapy(递归解析,post请求,日志等级,请求传参)
- 持续集成(CI):WEB自动化+Allure+Jenkins定时构建