hdu 2858 ac自动机 +状态压缩dp

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

题意: 给你n m k 给你m个单词 让你求有多少种长度为n的串,使得m个单词中含有k个在里面
m的数量较少 用ac自动机+状态压缩dp

dp[i][j][S] 分别是 构造的串的第i个字符,j是指在字典树上的哪个点,那么next[j][t] 代表j可以到达t,所以j的数量累加到t上。同时更新状态S|=t[next][j].end;(单词的末尾点)


#include <bits/stdc++.h>
using namespace std;
#define M 26*10+2
typedef long long ll;
struct trie{
    int end;//是否为该单词的最后一个结点
    int fail;//失配指针
    int next[26];//26个字母方向的子结点
}t[M];
int q[M],head,tail,L;
const ll MOD = 20090717;
char s[11][26];

ll dp[30][M][(1<<10)+10];

int Insert(char *a,int id)//将单词插入字典树
{
    int i=0,p=0,j,x;
    while(a[i]){
        x=t[p].next[a[i]-'a'];
        if(x<0){//前面字符串未访问过此处,则申请新结点
            t[p].next[a[i]-'a']=x=++L;//数组模拟链表申请新结点(即++L操作)
            for(j=0;j<26;j++)t[x].next[j]=-1;
            t[x].fail=-1;t[x].end=0;;//初始化新结点信息
        }
        p=x;
        i++;
    }
    t[x].end|=(1<<id);

    return x;
}
void build_ACauto()//更新失配指针
{
    int i,x,y,p;
    t[0].fail=-1;
    q[tail++]=0;//将根放入队列
    while(head<tail){
        x=q[head++];//取队首元素
        if(t[x].fail>0)
        {
        t[x].end|=t[t[x].fail].end;
        }
        for(i=0;i<26;i++){
            y=t[x].next[i];
            if(y>=0){
                if(!x)t[y].fail=0;//如果x为根结点,那么他的子结点的失配指针为头结点
                else{
                    p=t[x].fail;//取父结点的失配指针
                    while(p>=0){//如果失配指针不为空,继续找
                        if(t[p].next[i]>=0){//如果找到结点与相配
                            t[y].fail=t[p].next[i];//将失配指针指向它后退出循环
                            break;
                        }
                        p=t[p].fail;//否则继续往上找
                    }
                    if(p<0)t[y].fail=0;//如果最终还是没有找到,则失配指针指向根结点
                }
                q[tail++]=y;//将子结点存入队尾
            }
            else t[x].next[i]=t[t[x].fail].next[i];
        }
    }
}

ll solve(int n,int m,int k)
{
    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=L;j++)
        {
            for(int S=0;S<(1<<m);S++)
                dp[i][j][S]=0;
        }
    }
    dp[0][0][0]=1;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=L;j++)
        {
            for(int S=0;S<(1<<m);S++)
            {
                if(dp[i][j][S]<=0) continue;
                for(int tt=0;tt<26;tt++)
                {
                    int ni=i+1,nj=t[j].next[tt];
                    int nS=S|t[nj].end;
                    dp[ni][nj][nS]=(dp[ni][nj][nS]+dp[i][j][S])%MOD;                
                }
            }
        }
    }

    ll res=0;
    for(int S=0;S<(1<<m);S++)
    {
        int cnt=0;
        for(int i=0;i<m;i++)
            if(S&(1<<i)) ++cnt;
        if(cnt>=k)
        {
            for(int i=0;i<=L;i++)
                res=(res+dp[n][i][S])%MOD;
        }
    }
    return res;
}



int main()
{

        int n,m,k;
        while(scanf("%d%d%d",&n,&m,&k),n||m||k)
        {
        head=tail=L=0;
        t[0].fail=-1;//初始化头结点信息

        for(int i=0;i<26;i++)t[0].next[i]=-1;
        for(int i=0;i<m;i++)
        {
            scanf(" %s",s[i]);
            Insert(s[i],i);
        }

        build_ACauto();
        printf("%lld\n",solve(n,m,k) );
        }
        return 0;
}
posted @ 2017-09-19 21:18  黑码的博客  阅读(117)  评论(0编辑  收藏  举报