题目描述:
JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版。该软件可以随机生成一些文章―――总是生成一篇长度固定且完全随机的文章—— 也就是说,生成的文章中每个字节都是完全随机的。如果一篇文章中至少包含使用者们了解的一个单词,那么我们说这篇文章是可读的(我们称文章a包含单词b,当且仅当单词b是文章a的子串)。但是,即使按照这样的标准,使用者现在使用的GW文本生成器v6版所生成的文章也是几乎完全不可读的。 ZYX需要指出GW文本生成器 v6生成的所有文本中可读文本的数量,以便能够成功获得v7更新版。你能帮助他吗?
输入:输入文件的第一行包含两个正整数,分别是使用者了解的单词总数N (<= 60),GW文本生成器 v6生成的文本固定长度M;以下N行,每一行包含一个使用者了解的单词。 这里所有单词及文本的长度不会超过100,并且只可能包含英文大写字母A..Z 。
输出:
一个整数,表示可能的文章总数。只需要知道结果模10007的值。
样例输入:
2 2
A
B
样例输出:
100
题解:
构造AC自动机,然后在AC自动机上DP。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#ifdef CT
#define debug(...) printf(__VA_ARGS__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", 'r', stdin); freopen(filename".out", 'w', stdout)
#endif
#define R register
#define getc() (S==T&&(T=(S=B)+fread(B,1,1<<15,stdin),S==T)?EOF:*S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
#define cabs(_x) ((_x)<0?(-_x):(_x))
char B[1<<15],*S=B,*T=B;
inline int FastIn()
{
R char ch;R int cnt=0;R bool minus=0;
while (ch=getc(),(ch < '0' || ch > '9') && ch != '-') ;
ch == '-' ?minus=1:cnt=ch-'0';
while (ch=getc(),ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
return minus?-cnt:cnt;
}
#define maxn 110
#define mod 10007
#define maxcnt 6010
int fail[maxcnt], a[maxcnt][26], cnt;
int dp[maxn][maxcnt], q[maxcnt];
char str[maxn];
bool end[maxcnt];
inline void Insert()
{
R int now = 0;
for (R int i = 0; str[i]; ++i)
if (!a[now][str[i] - 'A']) now = a[now][str[i] - 'A'] = ++cnt;
else now = a[now][str[i] - 'A'];
end[now] = 1;
}
inline void ACmach()
{
fail[0] = 0;
R int head = 0, tail = 0;
for (R int i = 0; i < 26; ++i)
if (a[0][i]) q[++tail] = a[0][i];
while (head < tail)
{
head++;
R int now = q[head];
for (R int i = 0; i < 26; ++i)
if (a[now][i])
{
R int k = fail[now];
while (!a[k][i] && k) k = fail[k];
fail[a[now][i]] = a[k][i];
if (end[a[k][i]])
end[a[now][i]] = 1;
q[++tail] = a[now][i];
}
}
}
inline int qpow(R int x, R int power)
{
R int res = 1;
for (; power; power >>= 1, x = (x * x) % mod)
power & 1 ? res = (res * x) % mod : 0;
return res;
}
int main()
{
//setfile();
R int n, m;
scanf("%d %d\n", &n, &m);
for (R int i = 1; i <= n; ++i)
{
scanf("%s",str);
Insert();
}
ACmach();
dp[0][0] = 1;
for (R int i = 1; i <= m; ++i)
for (R int j = 0; j <= cnt; ++j)
if (!end[j] && dp[i - 1][j])
{
for (R int jj = 0; jj < 26; ++jj)
{
R int k = j;
while (!a[k][jj] && k) k = fail[k];
dp[i][a[k][jj]] = (dp[i][a[k][jj]] + dp[i - 1][j]) % mod;
}
}
R int tot = qpow(26, m), tmp = 0;
for (R int i = 0; i <= cnt; ++i)
if (!end[i]) tmp = (tmp + dp[m][i]) % mod;
printf("%d\n",(tot - tmp + mod) % mod );
return 0;
}