bzoj1030 [JSOI2007]文本生成器

时间:2023-03-08 19:16:11

1030: [JSOI2007]文本生成器

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 2654  Solved: 1100
[Submit][Status][Discuss]

Description

JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版。该软件可以随机生成一些文章―――总是生成一篇长度固定且完全随机的文章—— 也就是说,生成的文章中每个字节都是完全随机的。如果一篇文章中至少包含使用者们了解的一个单词,那么我们说这篇文章是可读的(我们称文章a包含单词b,当且仅当单词b是文章a的子串)。但是,即使按照这样的标准,使用者现在使用的GW文本生成器v6版所生成的文章也是几乎完全不可读的。 ZYX需要指出GW文本生成器 v6生成的所有文本中可读文本的数量,以便能够成功获得v7更新版。你能帮助他吗?

Input

输入文件的第一行包含两个正整数,分别是使用者了解的单词总数N (<= 60),GW文本生成器 v6生成的文本固定长度M;以下N行,每一行包含一个使用者了解的单词。 这里所有单词及文本的长度不会超过100,并且只可能包含英文大写字母A..Z  。

Output

一个整数,表示可能的文章总数。只需要知道结果模10007的值。

Sample Input

2 2
A
B

Sample Output

100

HINT

Source

题意:给出n串字符串ai(1<=i<=n<=60),问有多少种长度为m的字符串至少包含一个ai

分析:这种题很经典,就是Ac自动机+Dp,数据还很小,暴力Dp即可

建出Ac自动机后有两种方法统计

1、Dp[2][n][n*m](最多有n*m个节点)第一维表示是否访问过危险节点(即是否包含ai)第二维表示这个长度为m的字符串现在是第几位,第三维表示当前在AC自动机上的第几个节点Dp[2][i][j]表示第i步走到第j个节点的方案数,最后统计答案即可

2、我的代码是第二种方法的,求有多少种包含的,利用补集思想,即求总方案数-不包含的方案数,求不包含的方案数就是更加经典的AC自动机dp,dp[i][j]表示第i步走到第j个节点不碰到危险节点的方案数,总方案数显然为26^m,相减即可

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define Rep(i, t) for(int i = (0); i < (t); i++)
#define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
#define rep(i, x, t) for(int i = (x); i < (t); i++)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair
inline void SetIO(string Name) {
string Input = Name+".in",
Output = Name+".out";
freopen(Input.c_str(), "r", stdin),
freopen(Output.c_str(), "w", stdout);
} const int N = , M = , Mod = ;
struct Node {
int Child[], Fail;
bool Danger;
} Tr[N*M];
int n, m, Tot;
int Dp[M][N*M]; inline void Input() {
scanf("%d%d", &n, &m);
string Str;
For(i, , n) {
cin>>Str;
int Len = Str.length(), x = , ch;
Rep(j, Len) {
ch = Str[j]-'A';
if(!Tr[x].Child[ch]) Tr[x].Child[ch] = ++Tot;
x = Tr[x].Child[ch];
}
Tr[x].Danger = ;
}
} queue<int> Que;
inline void Build() {
Que.push();
Tr[].Fail = -;
while(sz(Que)) {
int x = Que.front();
Que.pop();
Rep(c, ) {
int tmp = Tr[x].Child[c];
if(tmp) Que.push(tmp);
if(!x) continue;
for(int tab = Tr[x].Fail; tab != -; tab = Tr[tab].Fail)
if(Tr[tab].Child[c]) {
if(tmp) Tr[tmp].Fail = Tr[tab].Child[c];
else Tr[x].Child[c] = Tr[tab].Child[c];
break;
}
}
if(x) Tr[x].Danger |= Tr[Tr[x].Fail].Danger;
}
} inline void Solve() {
Build(); Dp[][] = ;
Rep(Step, m)
For(i, , Tot)
if(Dp[Step][i] && !Tr[i].Danger) {
Rep(c, ) {
Dp[Step+][Tr[i].Child[c]] += Dp[Step][i];
Dp[Step+][Tr[i].Child[c]] %= Mod;
}
} int Cnt = , Sum = , Ans;
For(i, , Tot)
if(!Tr[i].Danger) Cnt = (Cnt+Dp[m][i])%Mod;
Rep(i, m) Sum = (Sum*)%Mod;
Ans = (Sum-Cnt+Mod)%Mod;
printf("%d\n", Ans);
} int main() {
#ifndef ONLINE_JUDGE
SetIO("");
#endif
Input();
Solve();
return ;
}