"bzoj1030": [JSOI2007] Text generator string-ac automaton-DP

Source: Internet
Author: User

"bzoj1030": [JSOI2007] Text generator

First, the problem of matching any number of questions into the total number-no number of matches

First, the AC automata are constructed, and then the letters of each bit are enumerated, and the position on the automata

F[I][J] is the position of the I bit at J and does not match the number of any one string

Then 26^m-sum (F[m][j]) is the answer.

There is a p->fail to the root of the path as long as there is a point is a string of the end point so dot f[i][p] will ban off because this WA many times

1 /* http://www.cnblogs.com/karl07/ */2#include <cstdlib>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <algorithm>7 using namespacestd;8 9 #defineP 10007Ten structtrie{ OneTrie *next[ -],*fail; A     intth,w; -}t[6005],*root=t,*new=t,*q[6005]; -  the intN,m,l,r,ans; - intf[ the][6005]; - Chars[ the]; -  +Trie *new1 () {new++; new->th=new->w=0;returnNEW;} -  + #definePNF P->next[i]->fail A #definePN P->next[i] atTrie *insert (Trie *p,intIintW) { -     if(!PN) pn=New1 (); -pn->w|=W; -     returnPN; - } -  in voidBuild_fail () { -Trie *p=q[0]=Root; to      for(intI=0;i< -; i++)if(PN) Pnf=p,q[++r]=pn,pn->th=R; +      while(l<R) { -p=q[++l]; the          for(intI=0;i< -; i++){ *             if(PN) { $Q[++r]=pn,pn->th=R;Panax Notoginseng                  for(Pnf=p->fail; pnf!=root &&!pnf->next[i]; pnf=pnf->fail); -                 if(Pnf->next[i]) pnf=pnf->Next[i]; thePn->w|=pnf->W; +             } A         } the     } + } -  $ intQ_pow (intXinty) { $     intans=1; -      for(; y; x=x*x%p, y=y>>1)if(y&1) ans=ans*x%P; -     returnans; the } - voiddp () {Wuyif[0][0]=1; the      for(intk=0; k<m;k++){ -          for(intj=0; j<=r;j++)if(F[k][j] &&!q[j]->W) { Wu              for(intI=0;i< -; i++){ -Trie *p=Q[j]; About                  while(!PN && p!=root) p=p->fail; $                 if(PN) p=PN; -f[k+1][p->th]= (f[k+1][P-&GT;TH]+F[K][J])%P; -             } -         } A     } +      for(intI=0; i<=r;i++)if(!q[i]->w) ans= (Ans+f[m][i])%P; theprintf"%d\n", (Q_pow ( -, m)-ans+p)%P); - } $ #undefPn the #undefPnf the  the intMain () { thescanf"%d%d",&n,&m); -      for(intI=1; i<=n;i++){ inscanf"%s", s); the         intj=0, l=strlen (s); the          for(Trie *p=root;j<l;j++) P=insert (p,s[j]-'A', j==l-1); About     } the Build_fail (); the DP (); the     return 0; +}
View Code

"bzoj1030": [JSOI2007] Text generator string-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.