"BZOJ-1030" Text generator ac automaton + DP

Source: Internet
Author: User

1030: [JSOI2007] Text generator time limit:1 Sec Memory limit:162 MB
submit:3253 solved:1330
[Submit] [Status] [Discuss] Description

Jsoi to the players zyx a task, the production of a "text generator" computer software: The user of the software is some low-young people, they are now using the GW Text Generator V6 version. The software can generate some random articles----always generate a fixed length and completely random article--that is, every byte in the generated article is completely random. If an article contains at least one word that the user understands, then we say this article is readable (we call the article A contains the word B when and only if the word b is a substring of article a). But even by this standard, the V6 version of the GW text generator that the user is using now is almost completely unreadable. ZYX need to indicate the number of readable text in all the text generated by the GW text Generator V6 so that the V7 update can be successfully obtained. Can you help him?

Input

The first line of the input file contains two positive integers, the total number of words that the user understands (<=), and the GW text generator v6 The resulting text fixed length m; The following n lines, each containing a word that the user understands. All words and text will not be longer than 100 and may contain only capital letters a. Z.

Output

An integer that represents the total number of possible articles. You only need to know the value of the result modulus 10007.

Sample Input2 2
A
BSample Output -Hintsource Solution

String, first think of AC automata, program number? Think of DP

Read in a string, build a trie tree, build a fail pointer

Direct DP to meet the number of scenarios does not seem easy, may wish to convert the idea

Application of the idea of the complement set, to find the number of solutions not satisfied with the total number of programs can be poor, then the DP

$f [i][j]$ represents a J point matched to the I position on the AC automaton, the transfer is not very difficult to think

Then use the fast power to calculate the total scheme number, enumerate $\sum f[m][i]$, do poor and for the answer

Note: For the point where the fail pointer points, if end, the current point is also end

Code
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;#defineP 10007#defineN 10010intsz,son[n][ -],fail[n],q[n],loc[n],f[ the][n],n,m,ans;BOOLEnd[n];voidClear () {sz=1;  for(intI=1; i<= -; i++) son[0][i]=1; } voidInsertChars[]) {     intx=1;  for(intI=0; I<strlen (s); i++)         {             if(!son[x][s[i]-'A'+1]) son[x][s[i]-'A'+1]=++sz,x=sz; Elsex=son[x][s[i]-'A'+1]; } End[x]=1; } voidBuildfail () {intHead=0, tail=1; q[0]=1; fail[1]=0;  while(head<tail) {             intnow=q[head++];  for(intI=1; i<= -; i++)                 {                     if(!son[now][i])Continue; intff=Fail[now];  while(!son[ff][i]) ff=FAIL[FF]; Fail[son[now][i]]=Son[ff][i]; if(End[son[ff][i]]) end[son[now][i]]=1; Q[tail++]=Son[now][i]; }         } } intQuick_pow (intXinty) {Long LongRe=1;  for(intI=y; I i>>=1, x=x*x%p)if(i&1) re=re*x%p; return(int) re; } intMain () {clear ();CharS[n]; scanf ("%d%d",&n,&m);  for(intI=1; i<=n; i++) scanf ("%s", s), insert (s);     Buildfail (); f[0][1]=1;  for(intI=1; i<=m; i++)          for(intj=1; j<=sz; J + +)             if(!end[j] && f[i-1][j]) for(intk=1; k<= -; k++)                     {                         inttmp=J;  while(!son[tmp][k]) tmp=Fail[tmp]; F[I][SON[TMP][K]]+=f[i-1][j]; F[I][SON[TMP][K]]%=p; }      for(intI=1; i<=sz; i++)          if(!end[i]) ans+=f[m][i],ans%=p; intTot=quick_pow ( -, M); printf ("%d\n", (tot-ans+p)%p); return 0; }

I think I have to learn a

"BZOJ-1030" 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.