Bzoj 1030 [JSOI2007] text generator (AC automaton +DP)

Source: Internet
Author: User

1030: [JSOI2007] Text generator time limit:1 Sec Memory limit:162 MB
submit:3059 solved:1255
[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 -

Ideas

AC Automatic Machine +DP

Add the word to the AC automaton, let F[i][j] the end of the article with the length of I is the number of nodes of the J node in the Automaton (call ~), is to find a path that does not pass through the word node in the automaton, DP statistics. Then use the total scheme number 26^m minus sigma{f[m][i], 0<=i<sz and val[i]==0}

Note If the suffix is a word node, the node is also marked as a word node.

Code

1#include <cstdio>2#include <queue>3#include <cstring>4#include <iostream>5 using namespacestd;6 7 Const intn= -+5, l= -+5;8 Const intnode = n*l,mod=1e4+7;9 Const intSigma = -;Ten  One structacauto{ A     intCh[node][sigma],f[node],val[node],sz; -     voidClear () { -sz=1; memset (ch[0],0,sizeof(ch[0])); the     } -     voidInsertChar*s) { -         intN=strlen (s), u=0; -          for(intI=0; i<n;i++) { +             intc=s[i]-'A'; -             if(!Ch[u][c]) { +memset (Ch[sz],0,sizeof(Ch[sz])); Aval[sz]=0; ch[u][c]=sz++; at             } -u=Ch[u][c]; -         } -val[u]=1; -     } -     voidGet_fail () { inqueue<int>Q; -f[0]=0; to          for(intC=0; c<sigma;c++)  +             if(ch[0][C]) f[ch[0][c]]=0, Q.push (ch[0][c]); -          while(!Q.empty ()) { the             intR=Q.front (); Q.pop (); *              for(intC=0; c<sigma;c++) { $                 intU=CH[R][C];if(!u)Continue;Panax NotoginsengQ.push (U);intv=F[r]; -                  while(V&&!ch[v][c]) v=F[v]; the                 if(Val[ch[v][c]]) val[u]=1; +f[u]=Ch[v][c]; A             } the         } +      } - }ac; $  $ CharS[L];intN,m;intD[l][node]; -  - intMain () { thescanf"%d%d",&n,&m); - ac.clear ();Wuyi      for(intI=0; i<n;i++) { thescanf"%s", s); Ac.insert (s); -     } Wu Ac.get_fail (); -d[0][0]=1; About      for(intI=1; i<=m;i++) $          for(intj=0; j<ac.sz;j++)if(!ac.val[j]&&d[i-1][j]) { -              for(intC=0; c<sigma;c++) { -                 intK=j; while(!ac.ch[k][c]&&k) k=Ac.f[k]; -D[i][ac.ch[k][c]]= (d[i-1][J]+D[I][AC.CH[K][C])%MOD; A             } +         } the     intans1=0, ans2=1; -      for(intI=1; i<=m;i++) ans2= (ans2* -)%MOD; $      for(intI=0; i<ac.sz;i++) the         if(!ac.val[i]) ans1= (Ans1+d[m][i])%MOD; theprintf"%d", (ans2-ans1+mod)%MOD); the     return 0; the}

Bzoj 1030 [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.