HDU 2825 Wireless Password(自動機+DP),hdu2825
Problem DescriptionLiyuan 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.
InputThere 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.
OutputFor each test case, please output the number of possible passwords MOD 20090717.
Sample Input
10 2 2hello world 4 1 1icpc 10 0 00 0 0
Sample Output
2114195065
自動機+dp:設dp[i][j]k]為長度為i,到達自動機j節點,字串包含情況為k的方法數。則dp[0][0][0]=1;
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;const int MOD=20090717;int dp[30][110][1<<10];int n,m,k;int cal(int x){ int cnt=0; for(int i=0;i<10;i++) if(x&(1<<i)) cnt++; return cnt;}struct AC{ int next[110][26]; int fail[110]; int ed[110]; int root,L; int newnode() { for(int i=0;i<26;i++) next[L][i]=-1; ed[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++) { int id=buf[i]-'a'; if(next[now][id]==-1) next[now][id]=newnode(); now=next[now][id]; } ed[now]|=(1<<id); } void Build_AC() { 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(); ed[now]|=ed[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() { CLEAR(dp,0); dp[0][0][0]=1; for(int i=0;i<n;i++) { for(int j=0;j<L;j++) { for(int k=0;k<(1<<m);k++) { if(dp[i][j][k]) { for(int x=0;x<26;x++) { int id=next[j][x]; int st=k|ed[id]; dp[i+1][id][st]+=dp[i][j][k]; dp[i+1][id][st]%=MOD; } } } } } int ans=0; for(int i=0;i<(1<<m);i++) { if(cal(i)>=k) { for(int j=0;j<L;j++) ans=(ans+dp[n][j][i])%MOD; } } printf("%d\n",ans); }};AC A;int main(){ char str[20]; while(~scanf("%d%d%d",&n,&m,&k)) { if(n+m+k==0) break; A.init(); for(int i=0;i<m;i++) { scanf("%s",str); A.Insert(str,i); } A.Build_AC(); A.solve(); } return 0;}/**/