hdu2825(ac自动机,状态压缩dp)

202 篇文章 0 订阅
154 篇文章 0 订阅

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的我因为hdu2852和hdu4057这俩题彻底把我没学的数据结构里面的ac自动机搞懂了(无语),顺便把规范化的模板都写好了。。。

题意:

某人要去破译wifi密码。现在他已经知道密码全部由小写字母组成。且密码的长度n(1<=n<=25)。和m(0<=m<=10)个密码中可能出现的串(magic)。每个串长度不超过10.现在他已经知道密码中这m个串至少出现了k个。可以覆盖出现。现在问你密码可能有多少种。

如果做过hdu4057了这题应该很好写而且思路也明确的。

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\n",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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值