bzoj2938: [Poi2000]病毒
生活随笔
收集整理的這篇文章主要介紹了
bzoj2938: [Poi2000]病毒
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
被Star_Feel大爺帶著做題
明顯大力AC機然后找環(huán)
本來我一開始想的是先去有另一個病毒為前綴的病毒,結(jié)果今天早上寫的時候偷懶沒寫
結(jié)果跳fail的時候會跳到中間。。。無語,Star_Feel大爺叫我son或一下now和fail就過了
還有神仙是直接把fail接到兒子的更流弊了。。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std;struct Trie {int w[2],fail; bool ed; }tr[31000];int trlen; char ss[31000]; void insert() {int now=0; int len=strlen(ss+1);for(int i=1;i<=len;i++){int x=ss[i]-'0';if(tr[now].w[x]==0)tr[now].w[x]=++trlen;now=tr[now].w[x];}tr[now].ed=true; } int list[31000]; void bfs() {int head=1,tail=2;list[1]=0;while(head!=tail){int now=list[head];for(int x=0;x<=1;x++){int son=tr[now].w[x];if(son==0)continue;tr[son].ed|=tr[now].ed;if(now==0)tr[son].fail=0;else{int p=tr[now].fail;while(p!=0&&tr[p].w[x]==0)p=tr[p].fail;tr[son].fail=tr[p].w[x];tr[son].ed|=tr[tr[son].fail].ed;}list[tail++]=son;}head++;} }//------------------------AC---------------------------------------int v[31000]; bool dfs(int now) {if(tr[now].ed==true)return false;if(v[now]>=0)return v[now];v[now]=1;for(int x=0;x<=1;x++){int p=now;while(p!=0&&tr[p].w[x]==0)p=tr[p].fail;if(dfs(tr[p].w[x]))return true;}v[now]=0;return false; } int main() {freopen("a.in","r",stdin);freopen("a.out","w",stdout);int n;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%s",ss+1), insert();bfs();memset(v,-1,sizeof(v));if(dfs(0))printf("TAK\n");else printf("NIE\n");return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/AKCqhzdy/p/9727457.html
總結(jié)
以上是生活随笔為你收集整理的bzoj2938: [Poi2000]病毒的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求婚大作战剧情介绍
- 下一篇: Swift5.1 语言指南(九) 闭包