[bzoj1030] [JSOI2007] Text generator

Source: Internet
Author: User

AC Automaton Entry question ....

However, I just want to get started in case koi the question (in fact, the IQ mishap) ... yy feel the AC automaton is still normal yet write up on a variety of kneeling

It is clear that the total number of strings minus the number of unreadable strings ... The number of unreadable strings is the number of paths on the AC automaton that walk m (starting from the root) and do not pass through any of the complete recognizable words.

"Without any one full recognizable word" means that each node that goes is not the end node, and the fail pointer is not the end node. (This ensures that the path from the root to the node does not contain the complete word)

Build the AC automaton and then the DP

F[I][J] Indicates the number of paths that took the I step and finally on the J node of the automaton.

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5 using namespacestd;6 Const intsxt=10007;Const intmaxn= the;Const intmaxlen=103;7 8 intfail[maxn*maxlen],ch[maxn*maxlen][ -],dl[maxn*Maxlen],tot,len,l,r;9 BOOLTag[maxn*maxlen];//is the end node, or fail points to the end nodeTen CharS[maxlen]; One  A intf[2][maxn*Maxlen],pre,now; - intI,j,k,n,m,ans; -  theInlinevoidInsertintLen) { -RegisterintI,now; -      for(i=1; i<=len;i++) s[i]-='A'; -      for(now=0, i=1; i<=len;i++) +now=ch[now][s[i]]?ch[now][s[i]]:ch[now][s[i]]=++tot; -tag[now]=1; + } AInlinevoidGetfail () { atRegisterinti,j,tmp; -L=0, r=1, dl[1]=0; -      while(l<R) -          for(j=0,i=dl[++l];j< -; j + +)if(Ch[i][j]) { -              for(tmp=fail[i];tmp&&!ch[tmp][j];tmp=fail[tmp]); -FAIL[CH[I][J]]=I?CH[TMP][J]:0, intag[ch[i][j]]|=Tag[ch[tmp][j]]; -dl[++r]=Ch[i][j]; to         }     + } -  the intMain () { *Registerintj,k,tmp; $scanf"%d%d",&n,&m);Panax Notoginseng      for(i=ans=1; i<=m;i++){ -ans*= -;if(ANS&GT;=SXT) ans%=SXT; the     } +      for(i=1; i<=n;i++){ A          for(s[len=1]=getchar (); s[1]<'A'|| s[1]>'Z'; s[1]=GetChar ()); the          while(s[len]>='A'&&s[len]<='Z') s[++len]=GetChar (); +len--, insert (len); -     } $ Getfail (); $f[0][0]=1; -      for(i=1, now=1, pre=0; i<=m;i++, Swap (now,pre)) -          for(Memset (F[now),0, (tot+1) <<2), j=dl[l=1];L&LT;=R;J=DL[++L])if(!tag[j]&&F[pre][j]) the          for(k=0;k< -; k++){ -              for(tmp=j;tmp&&!ch[tmp][k];tmp=fail[tmp]);Wuyitmp=Ch[tmp][k]; the             if(!tag[tmp]) -F[NOW][TMP]+=F[PRE][J],F[NOW][TMP]-=F[NOW][TMP]&GT;=SXT?SXT:0; Wu         } -      for(i=0; i<=tot;i++) Aboutans-=f[pre][i],ans+=ans<0? SXT:0; $printf"%d\n", ans); -     return 0; -}
View Code

The code runs slowly and without readability qaq

[bzoj1030] [JSOI2007] Text generator

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.