不要被状压吓倒了。。和之前用AC自动机求构造字符串的数量方法类似,但是这个问题要多开一维状态表示包含多少特殊的串。
dp[i][j][k]表示长度为i已经匹配到j的串包含特殊串的状态为k的数量。
转移方程是dp[i][u][s|ed[u]] += dp[i-1][j][s](u = j->ch)
#include<bits/stdc++.h>
using namespace std;
char ss[11];
const int mod = 20090717;
struct AC{
int nex[500][26], root, tot;
int f[500], ed[500];
void init() {
tot = 0;
root = newnode();
}
int newnode() {
for(int i = 0; i < 26; i++) {
nex[tot][i] = -1;
}
ed[tot] = 0;
return tot++;
}
void insert(char *s, int x) {
int u = root, len = strlen(s);
for(int i = 0; i < len; i++) {
int ch = s[i]-'a';
if(nex[u][ch] == -1) nex[u][ch] = newnode();
u = nex[u][ch];
}
ed[u] = 1<<(x-1);
}
void getfail() {
queue<int>Q;
for(int i = 0; i < 26; i++) {
if(nex[root][i] == -1) nex[root][i] = root;
else {
f[nex[root][i]] = root;
Q.push(nex[root][i]);
}
}
while(!Q.empty()) {
int u = Q.front();Q.pop();
ed[u] |= ed[f[u]];
for(int i = 0; i < 26; i++) {
if(nex[u][i] == -1) nex[u][i] = nex[f[u]][i];
else {
f[nex[u][i]] = nex[f[u]][i];
Q.push(nex[u][i]);
}
}
}
}
}ac;
long long dp[30][105][1205];
int cnt[1025];
int main() {
for(int i = 0; i < 1024; i++) cnt[i] = __builtin_popcount(i);
int n, m, k;
while(scanf("%d%d%d", &n, &m, &k) == 3 && (n+m+k)) {
ac.init();
for(int i = 1; i <= m; i++) {
scanf("%s", ss);
ac.insert(ss, i);
}
ac.getfail();
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
int M = 1<<m;
for(int i = 0; i <= n; i++) {
for(int j = 0; j < ac.tot; j++) {
for(int a = 0; a < M; a++) {
if(!dp[i][j][a]) continue;
for(int g = 0; g < 26; g++) {
int t = ac.nex[j][g];
dp[i+1][t][ac.ed[t]|a] += dp[i][j][a];
dp[i+1][t][ac.ed[t]|a] %= mod;
}
}
}
}
int res = 0;
for(int i = 0; i < ac.tot; i++) {
for(int j = 0; j < M; j++) {
if(cnt[j] >= k) res += dp[n][i][j], res %= mod;
}
}
printf("%d\n", res);
}
}