Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless network in the building. Liyuan did not know the password of the network, but he got some important information from his neighbor. He knew the password consists only of lowercase letters ‘a’-‘z’, and he knew the length of the password. Furthermore, he got a magic word set, and his neighbor told him that the password included at least k words of the magic word set (the k words in the password possibly overlapping).
For instance, say that you know that the password is 3 characters long, and the magic word set includes ‘she’ and ‘he’. Then the possible password is only ‘she’.
Liyuan wants to know whether the information is enough to reduce the number of possible passwords. To answer this, please help him write a program that determines the number of possible passwords.
Input
There will be several data sets. Each data set will begin with a line with three integers n m k. n is the length of the password (1<=n<=25), m is the number of the words in the magic word set(0<=m<=10), and the number k denotes that the password included at least k words of the magic set. This is followed by m lines, each containing a word of the magic set, each word consists of between 1 and 10 lowercase letters ‘a’-‘z’. End of input will be marked by a line with n=0 m=0 k=0, which should not be processed.
Output
For each test case, please output the number of possible passwords MOD 20090717.
Sample Input
10 2 2
hello
world
4 1 1
icpc
10 0 0
0 0 0
Sample Output
2
1
14195065
题意: 给你n m k 给你m个单词 让你求有多少种长度为n的串,使得m个单词中含有k个在里面
m的数量较少 用ac自动机+状态压缩dp
dp[i][j][S] 分别是 构造的串的第i个字符,j是指在字典树上的哪个点,那么next[j][t] 代表j可以到达t,所以j的数量累加到t上。同时更新状态S|=t[next][j].end;(单词的末尾点)
#include <bits/stdc++.h>
using namespace std;
#define M 26*10+5
const int SIGMA_SIZE = 26;
typedef long long ll;
const ll MOD = 20090717;
struct AC
{
ll end[M];
ll dp[30][M][(1<<10)+2];
int ch[M][26];
int val[M];
int height[M];
int fail[M],last[M];
int sz;
void clear(){memset(ch[0],0,sizeof(ch[0]));sz=0;}
int idx(char x){return x-'a';}
int insert(char *s,int id)
{
int u=0;
int n=strlen(s);
for(int i=0;i<n;i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
ch[u][c]=++sz;
end[sz]=0;
memset(ch[sz],0,sizeof(ch[sz]));
height[sz]=height[u]+1;
val[sz]=0;
}
u=ch[u][c];
}
val[u]++;
end[u]|=(1<<id);
return u;
}
void getfail()
{
queue<int> q;
fail[0]=0;
int u=0;
for(int i=0;i<SIGMA_SIZE;i++)
{
u=ch[0][i];
if(u){q.push(u);fail[u]=0;last[u]=0;}
}
while(!q.empty())
{
int r=q.front();q.pop();
end[r]|=end[fail[r]];
for(int i=0;i<SIGMA_SIZE;i++)
{
u=ch[r][i];
if(!u){ch[r][i]=ch[fail[r]][i];continue;}
q.push(u);
int v=fail[r];
while(v&&!ch[v][i])v=fail[v];
fail[u]=ch[v][i];
last[u]=val[fail[u]]?fail[u]:last[fail[u]];
}
}
}
ll solve(int n,int m,int k)
{
for(int i=0;i<=n;i++)
{
for(int j=0;j<=sz;j++)
{
for(int S=0;S<(1<<m);S++)
dp[i][j][S]=0;
}
}
dp[0][0][0]=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<=sz;j++)
{
for(int S=0;S<(1<<m);S++)
{
if(dp[i][j][S]<=0) continue;
for(int tt=0;tt<26;tt++)
{
int ni=i+1,nj=ch[j][tt];
int nS=S|end[nj];
dp[ni][nj][nS]=(dp[ni][nj][nS]+dp[i][j][S])%MOD;
}
}
}
}
ll res=0;
for(int S=0;S<(1<<m);S++)
{
int cnt=0;
for(int i=0;i<m;i++)
if(S&(1<<i)) ++cnt;
if(cnt>=k)
{
for(int i=0;i<=sz;i++)
res=(res+dp[n][i][S])%MOD;
}
}
return res;
}
}tree;
char s[30];
int main()
{
int n,m,k;
while(scanf("%d%d%d",&n,&m,&k),n||m||k)
{
tree.clear();
for(int i=0;i<m;i++)
scanf(" %s",s),tree.insert(s,i);
tree.getfail();
printf("%lld\n",tree.solve(n,m,k) );
}
return 0;
}