AC自动机+状态压缩DP+hdu2825

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
BestCoder官方QQ群:385386683 欢迎加入~
寻人启事:2014级新生看过来!

Wireless Password

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4381    Accepted Submission(s): 1323


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的字符串,其中至少包含k个给出的串,文有多少种构造方式

思路:这个跟前面的几个题很像,前面的是构造的串不包含给出的字符串,可以矩阵或者dp,这里可以用状态压缩,记录下构造出来的串包含哪些字符串,就跟前面一样了

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=110;
const int MOD=20090717;
const int SIGMA_SIZE=26;
int N,M,K;
char word[20];
int dp[30][110][1<<10],num[5000];

struct AC
{
    int ch[maxn][SIGMA_SIZE];
    int fail[maxn];
    int val[maxn];
    int sz;
    void clear(){memset(ch[0],0,sizeof(ch[0]));sz=1;}
    int idx(char x){return x-'a';}
    void insert(char *s,int id)
    {
        int u=0,n=strlen(s);
        for(int i=0;i<n;i++)
        {
            int c=s[i]-'a';
            if(!ch[u][c])
            {
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]|=(1<<id);
    }
    void getfail()
    {
        queue<int> q;
        int u=0;
        fail[0]=0;
        for(int i=0;i<SIGMA_SIZE;i++)
        {
            u=ch[0][i];
            if(u){fail[u]=0;q.push(u);}
        }
        while(!q.empty())
        {
            int r=q.front();q.pop();
            val[r]|=val[fail[r]];//不要忘记这里
            for(int c=0;c<SIGMA_SIZE;c++)
            {
                u=ch[r][c];
                if(!u){ch[r][c]=ch[fail[r]][c];continue;}
                q.push(u);
                int v=fail[r];
                while(v&&!ch[v][c])v=fail[v];
                fail[u]=ch[v][c];
            }
        }
    }
    void solve()
    {
        for(int i = 0;i <= N;i++)
            for(int j = 0;j < sz;j++)
                for(int p = 0;p < (1<<M);p++)
                    dp[i][j][p]=0;
        //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 k=0;k<(1<<M);k++)
                {
                    if(dp[i][j][k]<=0)continue;
                    for(int c=0;c<SIGMA_SIZE;c++)
                    {
                        int p=(k|val[ch[j][c]]);
                        dp[i+1][ch[j][c]][p]+=dp[i][j][k];
                        dp[i+1][ch[j][c]][p]%=MOD;
                    }
                }
            }
        }
        int ans=0;
        for(int p=0;p<(1<<M);p++)
        {
            if(num[p]<K)continue;
            for(int i=0;i<sz;i++)
                ans=(ans+dp[N][i][p])%MOD;
        }
        printf("%d\n",ans);
    }
};
AC ac;
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)==3)
    {
        if(N==0&&M==0&&K==0)break;
        ac.clear();
        for(int i=0;i<M;i++)
        {
            scanf("%s",word);
            ac.insert(word,i);
        }
        ac.getfail();
        ac.solve();
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值