Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5400 Accepted Submission(s): 1704
Problem Description
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
题意:
给 m 个单词构成的集合,统计所有长度为 n 的串中,包含至少 k 个单词的方案数。
思路:先构造trie图,然后用状压dp。这里注意,在构造trie图的时候单词尾节点的val值要写成val[x]=val[x]|val[fail[x] ].
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 505
#define maxnode 205
#define MOD 20090717
int dp[30][105][1050];
struct trie{
int sz,root,val[maxnode],next[maxnode][30],fail[maxnode];
int q[1111111];
void init(){
int i;
sz=root=0;
val[0]=0;
for(i=0;i<26;i++){
next[root][i]=-1;
}
}
int idx(char c){
return c-'a';
}
void charu(char *s,int index){
int i,j,u=0;
int len=strlen(s);
for(i=0;i<len;i++){
int c=idx(s[i]);
if(next[u][c]==-1){
sz++;
val[sz]=0;
next[u][c]=sz;
u=next[u][c];
for(j=0;j<26;j++){
next[u][j]=-1;
}
}
else{
u=next[u][c];
}
}
val[u]|=(1<<index-1);
}
void build(){
int i,j;
int front,rear;
front=1;rear=0;
for(i=0;i<26;i++){
if(next[root][i]==-1 ){
next[root][i]=root;
}
else{
fail[next[root][i] ]=root;
rear++;
q[rear]=next[root][i];
}
}
while(front<=rear){
int x=q[front];
val[x]|=val[fail[x] ];
front++;
for(i=0;i<26;i++){
if(next[x][i]==-1){
next[x][i]=next[fail[x] ][i];
}
else{
fail[next[x][i] ]=next[fail[x] ][i];
rear++;
q[rear]=next[x][i];
}
}
}
}
}ac;
int panduan(int state,int k){
int i,j,num;
num=0;
while(state){
if(state%2==1)num++;
state>>=1;
}
if(num>=k)return 1;
return 0;
}
int main()
{
int n,m,i,j,k,state,state1,ii,jj,t;
char s[20];
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
if(n==0 && m==0 && k==0)break;
ac.init();
for(i=1;i<=m;i++){
scanf("%s",s);
ac.charu(s,i);
}
ac.build();
memset(dp,0,sizeof(dp));
dp[0][0][0]=1;
for(i=0;i<=n-1;i++){
for(j=0;j<=ac.sz;j++){
for(state=0;state<=((1<<m)-1);state++ ){
if(dp[i][j][state]){
for(t=0;t<26;t++){
int ii=i+1;
int jj=ac.next[j][t];
int state1=(state| ac.val[ac.next[j][t] ] );
dp[ii][jj][state1]=(dp[ii][jj][state1]+dp[i][j][state])%MOD;
}
}
}
}
}
int sum=0;
for(state=0;state<=((1<<m)-1);state++){
if(panduan(state,k)){
for(j=0;j<=ac.sz;j++){
sum+=dp[n][j][state];
sum%=MOD;
}
}
}
printf("%d\n",sum);
}
return 0;
}