hdu2825Wireless Password【ac自动机+dp状态压缩】

Total Submission(s): 5502    Accepted Submission(s): 1737


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
 

Source
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:   2819  2824  2817  2823  2822 
 

扔了大半年的Ac自动机再见全还回去了,特别不理解先做第一题的人的心理,第一题还得状态压缩,这题就不用压啊

说题意:邻居wifi密码由n个字符组成,由m个子字符串的至少k个组成,问有多少种排列组合的方法。首先复习了一下,AC自动机,next[L][26]  fail[L]  end[L]分别表示下一个节点的指针,失败指针类似于kmp的next指针,计数的数组(这道题里这个数组不是直接++的,下文会说)

一共有init()  insert() build() solve()这些函数,除了最后一个,都是Ac自动机上的模板,大体结构不会变==最后一个函数之前是用来求个数的,其实这次也是,但是需要用dp==

dp[i][j][k]表示长度为i匹配到状态j 各个word出现的情况为k的方法数,解释一下什么意思~i,没得说 1~len;j是状态0~L;k要好好说一下,这个题还是用状压了的,它需要加一个数组num[],二进制中的每一位表示某个单词是否出现过,最最开始的时候初始化使得num[i]储存的值是i中二进制下1的个数,即所包含的单词个数,这个i也就是dp的最后一维。再说说原本用来计数的数组,原本是可以直接++计数的,然而由于状态是相互转化的,所以所有涉及到的地方都是按位或~原本的统计函数是模式串的个数的,从头到尾跑一边,每次+end[]就好,然而这个题是三层状态转移其中第二层也是类似于刚刚说的

/**************
hdu2825
2016.3.15
**************/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define mod 20090717
int num[1<<11];
int n,m,k;
struct Trie
{
    int next[110][26],fail[110],end[110],dp[30][110][1<<10];
    int root,L;
    int newnode()
    {
        for(int i=0;i<26;i++)next[L][i]=-1;
        end[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++)
        {
            if(next[now][buf[i]-'a']==-1)
                next[now][buf[i]-'a']=newnode();
            now=next[now][buf[i]-'a'];
        }
        end[now]|=(1<<id);
    }
    void build()
    {
        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();
            end[now]|=end[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()
    {
        memset(dp,0,sizeof(dp));
        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]>0)
                    for(int x=0;x<26;x++)
                    {
                        int newi=i+1;
                        int newj=next[j][x];//!!
                        int newk=k|end[newj];
                        dp[newi][newj][newk]+=dp[i][j][k];
                        dp[newi][newj][newk]%=mod;
                    }
                }
            }
        }
        int ans=0;
        for(int i=0;i<(1<<m);i++)
        {
            if(num[i]<k) continue;
            for(int j=0;j<L;j++)
            {
                ans+=dp[n][j][i];
                ans%=mod;
            }
        }
        return ans;
    }
}ac;
char buf[200];
int main()
{
    //freopen("cin.txt","r",stdin);
    num[0]=0;
    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))
    {
        if(n==0&&m==0&&k==0) break;
        ac.init();
        for(int i=0;i<m;i++)
        {
            scanf("%s",buf);
            ac.insert(buf,i);
        }
        ac.build();
        printf("%d\n",ac.solve());
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值