HDU2825 Wireless Password (AC自动机+状态压缩DP)

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

现在要你推断一个长度==n的由小写字母构成的字符串S有多少种组成方式.其中这个S至少包含字典集合中的k个单词.字典集合中有m个单词并已给出.

该题要用带match的AC自动机。每个单词有一个编号,match[j]表示j节点表示的单词状态,比如j节点表示单词5和单词3,那么match[j]== 1<<5 | 1<<3。即该match[j]值的二进制形式正好有2位为1,是第3位和第5位。

 令d[i][j][k]=x表示当前走了i步,处于AC自动机的j号节点,且字符串中的单词出现情况为k(k的二进制形式表示集合)时的情况种数为x。

 d[i+1][j][k|match[j]]  += d[i][j1][k]    

从j1号节点能走到j号节点,且j号节点的后缀单词覆盖情况为match[j],那么从j1节点走到j节点后,总的单词覆盖情况就是 k|match[j] 了(想想是不是)。

初值为:d[0][0][0]=1,其他所有d都为0。

ans = d[n][i][k] 其中i为0到sz-1的任意节点,而集合k的二进制形式至少要包含K个1。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
#include<sstream>
using namespace std;
typedef long long ll;
typedef double ld;
const int MOD=20090717;
const int maxnode=100+10;
const int sigma_size=26;
int dp[30][maxnode][(1<<10)+100];//开始这么定义dp[][][1<<10+100],结果内存溢出出现了BUG错误
int num[(1<<10)+10];//num[i]=x表示i的二进制有x个1
int N,M,K;
int ch[maxnode][sigma_size];
int f[maxnode];
int match[maxnode];//此处的match是一个2进制位集合,不再是0或1了
int sz;
void init()
{
    sz=1;
    memset(ch[0],0,sizeof(ch[0]));
    f[0]=match[0]=0;
}
void insert(char *s,int v)
{
    int n=strlen(s),u=0;
    for(int i=0; i<n; i++)
    {
        int id=s[i]-'a';
        if(ch[u][id]==0)
        {
            ch[u][id]=sz;
            memset(ch[sz],0,sizeof(ch[sz]));
            match[sz++]=0;
        }
        u=ch[u][id];
    }
    match[u] |=1<<v;
}
void getFail()
{
    f[0]=0;
    queue<int> q;
    for(int i=0; i<sigma_size; i++)
    {
        int u=ch[0][i];
        if(u)
        {
            f[u]=0;
            q.push(u);
        }
    }
    while(!q.empty())
    {
        int r=q.front();
        q.pop();
        for(int i=0; i<sigma_size; i++)
        {
            int u=ch[r][i];
            if(!u)
            {
                ch[r][i]=ch[f[r]][i];
                continue;
            }
            q.push(u);
            int v=f[r];
            while(v && ch[v][i]==0)
                v=f[v];
            f[u] = ch[v][i];
            match[u] |= match[f[u]];
        }
    }
}
int solve()
{
    for(int i=0; i<=N; i++)
        for(int j=0; j<sz; j++)
            for(int st=0; st<(1<<M); st++)
                dp[i][j][st]=0;
    dp[0][0][0]=1;
    for(int i=0; i<N; i++) //当前行走的长度i
    {
        for(int j=0; j<sz; j++) //当前所在的j号节点
        {
            for(int st=0; st<(1<<M); st++)
                if(dp[i][j][st]>0)//当前状态st
                {
                    for(int k=0; k<sigma_size; k++)
                    {
                        dp[i+1][ch[j][k]][(st|match[ch[j][k]])] =(dp[i+1][ch[j][k]][(st|match[ch[j][k]])] + dp[i][j][st])%MOD ;
                    }
                }
        }
    }
    int ans=0;
    for(int st=0; st<(1<<M); st++)
        if(num[st]>=K)
            for(int i=0; i<sz; i++)
                ans = (ans +dp[N][i][st])%MOD;
    return ans;
}

int main()
{
    memset(num,0,sizeof(num));
    for(int st=1; st<(1<<10); st++)
    {
        for(int j=0; j<10; j++)
            if(st&(1<<j))
                num[st]++;
    }
    while(scanf("%d%d%d",&N,&M,&K)==3&&N)
    {
        if(N==0&&M==0&&K==0)
            break;
        init();
        for(int i=0; i<M; i++)
        {
            char str[100];
            scanf("%s",str);
            insert(str,i);
        }
        getFail();
        printf("%d\n",solve());
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值