Bzoj 2938 Poi2000 virus ac automata + topology sequencing

Source: Internet
Author: User

Question: given n 01 strings, ask if there is an infinitely long 01 string that does not contain any one of these n 01 strings

After building the trie map, you will be awarded a ring.

I was a jerk, and I ran a Dfs to get a ring 23333.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 30300using namespace Std;int n;char s[m];namespace aho_corasick_automaton{struct trie{trie *son[2],*fail;bool ed;short into;} *root,mempool[m],*c=mempool;void Insert (Trie *&p,char *pos) {if (!p) p=new (c + +) Trie;if (!*pos) {p->ed=true; return;} Insert (p->son[*pos-' 0 '],pos+1);} void Build_tree () {static Trie *q[m];int i,r=0,h=0;for (i=0;i<2;i++) if (Root->son[i]) (Q[++r]=root->son[i])- >fail=root;elseroot->son[i]=root;while (r!=h) {Trie *p=q[++h];for (i=0;i<2;i++) {if (P->son[i]) {p-> Son[i]->fail=p->fail->son[i];p->son[i]->ed|=p->son[i]->fail->ed;q[++r]=p->son[i];} Elsep->son[i]=p->fail->son[i];}}} BOOL Find_ring () {static Trie *temp,*q[m];int i,r=0,h=0,cnt=0;for (temp=mempool;temp<c;temp++) if (!temp->ed) {+ +) Cnt;for (i=0;i<2;i++) temp->son[i]->into++;} for (temp=mempool;temp<c;temp++) if (!temp->into&&!temp->ed)Q[++r]=temp;while (r!=h) {Trie *p=q[++h];for (i=0;i<2;i++) if (!--p->son[i]->into&&!p->son[i]- >ed) q[++r]=p->son[i];} return cnt!=r;}} int main () {using namespace Aho_corasick_automaton;int i;cin>>n;for (i=1;i<=n;i++) {scanf ("%s", s+1); Insert ( root,s+1);} Build_tree ();p UTS (find_ring ()? " TAK ":" NIE "); return 0;}


Bzoj 2938 Poi2000 virus ac automata + topology sequencing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.