BZOJ1030 [JSOI2007] text generator (AC automaton +DP)

Source: Internet
Author: User

The main idea: to find the length of $m$ in a string consisting of $ ' A '-' Z ' $ contains at least one of the number of n pattern strings given.

Portal

It seems that the non-nude AC automata topics are all running on the machine DP ...

The direct solution is not good, we can use $26^m$ minus the number of pattern strings.

Build the automaton, set f[i][j] means walk I step, now in the J number of the path of the node.

Then F[i][j] can be transferred f[i+1][son[j][k]].
Is the state of i+1 characters K.
Finally put all F[m][i] tired and is not readable string.

This is the online argument, presumably that is the meaning.

I wrote the pointer version of the trie, ugly, look at the above explanation is good.

1#include <cstring>2#include <cstdio>3#include <cmath>4#include <algorithm>5 #defineForu (i,x,y) for (int i=x;i<=y;i++)6 using namespacestd;7 Const intMod=10007;8 structtrie{9     intv,f[201];TenTrie *nxt[ -],*fail; One     voidInit () {v=0; Fail=null;foru (I,0, -) Nxt[i]=null;memset (F,0,sizeof(f)); A}*r,*q[10000],*ind[10000]; -  - intn,m,ans,ans2,cnt; the Charch[ $]; -  - voidAddChar*s) { -Trie *k=r,*p; +     intL=strlen (s); -Foru (I,0, L-1){ +         intid=s[i]-'A'; A         if(!k->Nxt[id]) { atp= (trie*)malloc(sizeof(Trie)); -P->init (); -k->nxt[id]=p; -k=p; -ind[++cnt]=p; -}Else{ inK=k->Nxt[id]; -         } to     } +k->v=1; - } the  * voidSetfail () { $Trie *k=r,*p;Panax Notoginseng     ints=1, t=0; -q[++t]=R; the      while(s<=t) { +k=q[s++]; AForu (I,0, -){ the             if(k->Nxt[i]) { +P=k->fail; -                  while(P&&!p->nxt[i]) p=p->fail; $K->nxt[i]->fail= (p?p->nxt[i]:r); $Q[++t]=k->Nxt[i]; -}Else -K->nxt[i]= (k!=r?k->fail->nxt[i]:r); the         } -         if(k!=r) k->v|=k->fail->v;//if the suffix of a string can be understood, then the string can be understood.Wuyi     } the } -  Wu voidWork () { -r->f[0]=1; AboutForu (I,1, M) { $Foru (J,0, CNT) { -             if(IND[J]-&GT;V)Continue; -Foru (k,0, -){ -ind[j]->nxt[k]->f[i]+=ind[j]->f[i-1]; Aind[j]->nxt[k]->f[i]%=MoD; +             } the         } -     } $Foru (I,0, CNT) the         if(!ind[i]->v) { theAns2+=ind[i]->F[m]; theans2%=MoD; the         } -ans=1; inForu (I,1, m) ans= (ans* -)%MoD; theprintf"%d\n", ((ans-ans2)%mod+mod)%MoD); the } About  the intMain () { theR= (trie*)malloc(sizeof(Trie)); theR->init (); +ind[0]=R; -scanf"%d%d",&n,&m); theForu (I,1nn) {Bayiscanf"%s", ch); the Add (CH); the     } - Setfail (); - Work (); the     return 0; the}

BZOJ1030 [JSOI2007] text generator (AC automaton +DP)

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.