hdu 2852(acオートマチック、状態圧縮dp)


Wireless Password
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5524    Accepted Submission(s): 1741
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

詳しくは以下をクリックしてください.http://blog.csdn.net/martinue/article/details/50895963(まったく同じタイプ)
acオートマチックと状態圧縮dpの連合バージョンは、もともと図論とdpを学んでいた私がhdu 2852とhdu 4057という2つの問題で私が学んでいないデータ構造の中のacオートマチックを徹底的に理解した(無言)、ついでに規範化されたテンプレートを全部書いた.
タイトル:
誰かがwifiパスワードを解読しに行きます.パスワードはすべて小文字で構成されていることを知っています.パスワードの長さn(1<=n<=25).およびm(0<=m<=10)個のパスワードに現れる可能性のある列(magic).各列の長さは10を超えない.今、彼はパスワードの中でこのm個の列が少なくともk個現れたことを知っている.表示を上書きできます.パスワードは何種類あるか聞いてみましょう.hdu 4057をやったことがあるなら、この問題は書きやすく、構想も明確だと思います.
dp[i][j][k]は、長さiのacオートマチックにおけるj番目のノードのマッチングが良好であることをk(kを2進数にした後1はその位置を表す語が存在し、0は存在しないことを示す)とする可能性のある総数を表す.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
const int CH =26,NODE = 115,MOD=20090717;
int m,n,k;
int idx(char x)
{
    return x-'a';
}
int ch[NODE][CH],f[NODE],val[NODE],sz,num[1<<10];
int node()
{
    memset(ch[sz],0,sizeof(ch[sz]));
    val[sz]=0;
    return sz++;
}
void init()
{
    sz=0;
    node();
    memset(f,0,sizeof(f));
}
void ins(char *s,int i)
{
    int u=0;
    for(; *s; s++)
    {
        int c=idx(*s);
        if(!ch[u][c])
            ch[u][c]=node();
        u=ch[u][c];
    }
    val[u]|=1<<i;
}
int queu[110];
void getfail()
{
    int l=0,r=0;
    queu[r++]=0;
    while(l<r)
    {
        int p=queu[l++];
        for(int i=0; i<CH; i++)
        {
            if(!ch[p][i])
                ch[p][i]=ch[f[p]][i];
            else
            {
                int q=ch[p][i];
                if(p)
                    f[q]=ch[f[p]][i];
                val[q]|=val[f[q]];
                queu[r++]=q;
            }
        }
    }
}
int dp[30][105][1<<10];
void solve()
{
    memset(dp,0,sizeof(dp));
    dp[0][0][0]=1;
    for(int i=0; i<n; i++)
        for(int j=0; j<sz; j++)
            for(int hh=0; hh<(1<<m); hh++)
                if(dp[i][j][hh])
                    for(int k=0; k<CH; k++)
                        dp[i+1][ch[j][k]][hh|val[ch[j][k]]]+=dp[i][j][hh],
                        dp[i+1][ch[j][k]][hh|val[ch[j][k]]]%=MOD;

    int ans=0;
    for(int j=0; j<(1<<m); j++)
    {
        if(num[j]<k)continue;
        for(int i=0; i<sz; i++)
            ans=(ans+dp[n][i][j])%MOD;
    }
    printf("%d
",ans); } 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)&&n+m+k) { init(); for(int i=0; i<m; i++) { char ss[105]; scanf("%s",ss); ins(ss,i); } getfail(); solve(); } return 0; }