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