HDU2825AC自动机+状压
//我感觉这题不如叫你不看代码就不知道题干在说啥,强烈吐槽
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.
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
http://blog.csdn.net/braketbn/article/details/50650341 //参考的代码
-------------------------------手写的代码
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,k,dp[30][108][1100],cnt[1100];
const int mod=20090717;
struct AC
{
int ch[108][26],fail[108],val[108],sz,rt;
void init()
{
sz=rt=0;
memset(ch[rt],-1,sizeof(ch[rt]));
}
void insert(char *str,int c)
{
int u=rt,len=strlen(str);
for(int i=0; i<len; ++i)
{
if(ch[u][str[i]-'a']==-1)
{
++sz;
memset(ch[sz],-1,sizeof(ch[sz]));
val[sz]=0;
ch[u][str[i]-'a']=sz;
}
u=ch[u][str[i]-'a'];
}
val[u]=1<<c;
}
void build()
{
queue<int>Q;
int u=rt;
for(int i=0; i<26; ++i)
{
if(ch[u][i]==-1) ch[u][i]=rt;
else
{
fail[ch[u][i]]=rt;
Q.push(ch[u][i]);
}
}
while(!Q.empty())
{
u=Q.front();
Q.pop();
val[u]|=val[fail[u]];
for(int i=0; i<26; ++i)
{
if(ch[u][i]==-1) ch[u][i]=ch[fail[u]][i];
else
{
fail[ch[u][i]]=ch[fail[u]][i];
Q.push(ch[u][i]);
}
}
}
}
} ac;
char s[55];
int main()
{
for(int i=0; i<1024; ++i) cnt[i]=__builtin_popcount(i);//黑科技
while(scanf("%d%d%d",&n,&m,&k),n||m||k)
{
ac.init();
for(int i=0; i<m; ++i)
{
scanf("%s",s);
ac.insert(s,i);
}
ac.build();
memset(dp,0,sizeof(dp));
dp[0][0][0]=1;
int ed=1<<m;
for(int i=0; i<=n; ++i) for(int j=0; j<=ac.sz; ++j) for(int k=0; k<ed; ++k)
{
if(!dp[i][j][k]) continue;
for(int l=0; l<26; ++l)
{
int u=ac.ch[j][l];
dp[i+1][u][ac.val[u]|k]+=dp[i][j][k];
dp[i+1][u][ac.val[u]|k]%=mod;
}
}
int ans=0;
for(int i=0; i<=ac.sz; ++i) for(int j=0; j<ed; ++j) if(cnt[j]>=k)
ans=(ans+dp[n][i][j])%mod;
printf("%d\n",ans);
}
}
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,k,dp[30][108][1100],cnt[1100];
const int mod=20090717;
struct AC
{
int ch[108][26],fail[108],val[108],sz,rt;
void init()
{
sz=rt=0;
memset(ch[rt],-1,sizeof(ch[rt]));
}
void insert(char *str,int c)
{
int u=rt,len=strlen(str);
for(int i=0; i<len; ++i)
{
if(ch[u][str[i]-'a']==-1)
{
++sz;
memset(ch[sz],-1,sizeof(ch[sz]));
val[sz]=0;
ch[u][str[i]-'a']=sz;
}
u=ch[u][str[i]-'a'];
}
val[u]=1<<c;
}
void build()
{
queue<int>Q;
int u=rt;
for(int i=0; i<26; ++i)
{
if(ch[u][i]==-1) ch[u][i]=rt;
else
{
fail[ch[u][i]]=rt;
Q.push(ch[u][i]);
}
}
while(!Q.empty())
{
u=Q.front();
Q.pop();
val[u]|=val[fail[u]];
for(int i=0; i<26; ++i)
{
if(ch[u][i]==-1) ch[u][i]=ch[fail[u]][i];
else
{
fail[ch[u][i]]=ch[fail[u]][i];
Q.push(ch[u][i]);
}
}
}
}
} ac;
char s[55];
int main()
{
for(int i=0; i<1024; ++i) cnt[i]=__builtin_popcount(i);//黑科技
while(scanf("%d%d%d",&n,&m,&k),n||m||k)
{
ac.init();
for(int i=0; i<m; ++i)
{
scanf("%s",s);
ac.insert(s,i);
}
ac.build();
memset(dp,0,sizeof(dp));
dp[0][0][0]=1;
int ed=1<<m;
for(int i=0; i<=n; ++i) for(int j=0; j<=ac.sz; ++j) for(int k=0; k<ed; ++k)
{
if(!dp[i][j][k]) continue;
for(int l=0; l<26; ++l)
{
int u=ac.ch[j][l];
dp[i+1][u][ac.val[u]|k]+=dp[i][j][k];
dp[i+1][u][ac.val[u]|k]%=mod;
}
}
int ans=0;
for(int i=0; i<=ac.sz; ++i) for(int j=0; j<ed; ++j) if(cnt[j]>=k)
ans=(ans+dp[n][i][j])%mod;
printf("%d\n",ans);
}
}