[POI2000] 病毒

42 篇文章 0 订阅
5 篇文章 0 订阅

题目描述:

qwq.

题目分析:

把病毒串构建一个Ac 自动机
然后我们在Ac自动机上DFS,如果能够DFS出一个无病毒环,即成功!

题目链接:

Luogu 2444

Ac 代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
const int maxm=1e6+100; 
int n;
std::string s1;
namespace AC_automaton{
    struct node{
        int ch[2];
        int end,fail;
    }st[maxm];
    int sz;
    std::queue <int> dl;
    inline void build(std::string s)
    {
        int now=0;
        int len=s.length();
        for(int i=0;i<len;i++)
        {
            if(!st[now].ch[s[i]-'0']) st[now].ch[s[i]-'0']=++sz;
            now=st[now].ch[s[i]-'0'];
        }
        st[now].end=1;
    }
    inline void make_fail()
    {
        for(int i=0;i<2;i++)
         if(st[0].ch[i]) st[st[0].ch[i]].fail=0,dl.push(st[0].ch[i]);
        while(!dl.empty())
        {
            int now=dl.front();
            dl.pop();
            st[now].end|=st[st[now].fail].end;
            for(int i=0;i<2;i++)
            {
                if(st[now].ch[i])
                {
                    st[st[now].ch[i]].fail=st[st[now].fail].ch[i];
                    dl.push(st[now].ch[i]);
                }
                else
                 st[now].ch[i]=st[st[now].fail].ch[i];
            }
        }
    }
}
using namespace AC_automaton;
bool vis1[maxm],vis2[maxm];
void DFS(int now)
{
    if(st[now].end) return;
    if(vis1[now])
    {
        printf("TAK\n");
        std::exit(0);
    }
    if(vis2[now]) return;
    vis1[now]=vis2[now]=1;
    for(int i=0;i<2;i++)
     if(st[now].ch[i]) DFS(st[now].ch[i]);
    vis1[now]=0;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        std::cin>>s1;
        build(s1);
    }
    make_fail();
    DFS(0);
    printf("NIE\n");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值