您的位置:首页 > 其它

hdu2825 AC自动机+状态压缩DP

2015-05-03 21:14 323 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2825

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

2
1
14195065

/**
hdu2825 AC自动机+状态压缩DP
题目大意:给定一些模式串,求能够造出多少长度为n的字符串,使其包含至少L个模式串
解题思路:建立自动机,每个串代表对应一个二进制位,由于模式串数量很少,我们把他压到一个数里面。
dp[i][j][p]表示长度为i以节点j结尾,并且含有字符串个数的状态为p时的个数
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int mod=20090717;
int n,m,k;
int dp[30][110][1<<10];
int num[5000];

struct Trie
{
int next[110][26],fail[110],end[110];
int root,L;
int newnode()
{
for(int i=0; i<26; i++)
{
next[L][i]=-1;
}
end[L++]=0;
return L-1;
}
void init()
{
L=0;
root=newnode();
}
void insert(char *buf,int id)
{
int len=strlen(buf);
int now=root;
for(int i=0; i<len; i++)
{
if(next[now][buf[i]-'a']==-1)
{
next[now][buf[i]-'a']=newnode();
}
now=next[now][buf[i]-'a'];
}
end[now]|=(1<<id);
}
void build()
{
queue<int>Q;
fail[root]=root;
for(int i=0; i<26; i++)
{
if(next[root][i]==-1)
next[root][i]=root;
else
{
fail[next[root][i]]=root;
Q.push(next[root][i]);
}
}
while(!Q.empty())
{
int now=Q.front();
Q.pop();
end[now]|=end[fail[now]];
for(int i=0; i<26; i++)
{
if(next[now][i]==-1)
next[now][i]=next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
int solve()
{
for(int i=0; i<=n; i++)
{
for(int j=0; j<L; j++)
{
for(int p=0; p<(1<<m); p++)
{
dp[i][j][p]=0;
}
}
}
dp[0][0][0]=1;
for(int i=0; i<n; i++)
{
for(int j=0; j<L; j++)
{
for(int p=0; p<(1<<m); p++)
{
if(dp[i][j][p]>0)
{
for(int x=0; x<26; x++)
{
int newi=i+1;
int newj=next[j][x];
int newp=(p|end[newj]);
dp[newi][newj][newp]+=dp[i][j][p];
dp[newi][newj][newp]%=mod;
}
}
}
}
}
int ans=0;
for(int p=0; p<(1<<m); p++)
{
if(num[p]<k)continue;
for(int i=0; i<L; i++)
{
ans=(ans+dp
[i][p])%mod;
}
}
return ans;
}
} ac;
char buf[20];
int main()
{
for(int i=0; i<(1<<10); i++)
{
num[i]=0;
for(int j=0; j<10; j++)
{
if(i&(1<<j))
num[i]++;
}
}
while(~scanf("%d%d%d",&n,&m,&k))
{
if(n==0&&m==0&&k==0)break;
ac.init();
for(int i=0; i<m; i++)
{
scanf("%s",buf);
ac.insert(buf,i);
}
ac.build();
printf("%d\n",ac.solve());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: