[BZOJ1030] [JSOI2007] Text generator
Question 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.
Input example
2 2 AB
Output example
100
Data size and conventions
See " Input "
Exercises
First, the AC automaton is constructed, so that f (i, j) is filled with I letter, now matches to the AC automaton node J, and has never appeared the complete word scheme number, each bit enumerates 26 kinds of circumstances to transfer.
The last 26m-∑f (M, j) is the answer.
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype > #include <algorithm>using namespace Std;int read () {int x = 0, f = 1; char c = GetChar (); while (!isdigit (c)) {if ( c = = '-') f =-1; c = GetChar (); }while (IsDigit (c)) {x = x * + C-' 0 '; c = GetChar ();} return x * f;} #define Maxnode 6010#define maxn 110#define maxa 26#define MOD 10007char s[maxn];int f[maxn][maxnode];struct AC {int ToT, RT, Ch[maxnode][maxa], Fail[maxnode], Q[maxnode], HD, Tl;bool val[maxnode];void init () {ToT = RT = 1;return;} void Insert (const char* S) {int n = strlen (s), u = rt;for (int i = 0; i < n; i++) {int x = s[i]-' A '; if (!ch[u][x]) ch[u ][x] = ++tot;u = Ch[u][x];} Val[u] = 1;return;} void BFs () {HD = TL = 0; Q[++TL] = Rt;while (HD < TL) {int u = q[++hd];for (int c = 0; c < Maxa; C + +) if (Ch[u][c]) {int v = ch[u][c], j = fail[ U];while (J &&!ch[j][c]) j = fail[j]; FAIL[V] = Ch[j][c]? CH[J][C]: Rt;val[v] |= val[fail[v]; Q[++TL] = V;} else ch[u][c] = ch[fail[u]][c];} return;}} Sol;int Main () {int n = read (), Len = Read (), Sol.init (), for (int i = 1; I <= n; i++) {scanf ("%s", s); Sol. Insert (S);} Sol.bfs (); f[0][1] = 1;int ans = 1;for (int i = 0; i < len; i++) {(ans *=)%= mod;for (int u = 1; u <= sol. ToT; u++) if (F[i][u] &&!sol.val[u]) for (int c = 0; c < Maxa; C + +) {int v = sol.ch[u][c]? Sol.ch[u][c]: 1;f[i+1][v] + = F[i][u];if (f[i+1][v] >= MoD) f[i+1][v]-= mod;}} for (int u = 1; u <= sol. ToT; u++) if (!sol.val[u]) {ans-= f[len][u];if (ans < 0) ans + = MOD;} printf ("%d\n", ans); return 0;}
[BZOJ1030] [JSOI2007] Text generator