码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2825 Wireless Password(自动机+DP)

时间:2015-04-21 01:51:59      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

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
 
自动机+dp:
设dp[i][j]k]为长度为i,到达自动机j节点,字符串包含情况为k的方法数。
则dp[0][0][0]=1;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
const int MOD=20090717;
int dp[30][110][1<<10];
int n,m,k;
int cal(int x)
{
    int cnt=0;
    for(int i=0;i<10;i++)
        if(x&(1<<i)) cnt++;
    return cnt;
}
struct AC{
     int next[110][26];
     int fail[110];
     int ed[110];
     int root,L;

     int newnode()
     {
         for(int i=0;i<26;i++)
            next[L][i]=-1;
         ed[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++)
         {
             int id=buf[i]-'a';
             if(next[now][id]==-1)
                next[now][id]=newnode();
             now=next[now][id];
         }
         ed[now]|=(1<<id);
     }
     void Build_AC()
     {
         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();
             ed[now]|=ed[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()
     {
         CLEAR(dp,0);
         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])
                     {
                         for(int x=0;x<26;x++)
                         {
                             int id=next[j][x];
                             int st=k|ed[id];
                             dp[i+1][id][st]+=dp[i][j][k];
                             dp[i+1][id][st]%=MOD;
                         }
                     }
                 }
             }
         }
         int ans=0;
         for(int i=0;i<(1<<m);i++)
         {
             if(cal(i)>=k)
             {
                 for(int j=0;j<L;j++)
                    ans=(ans+dp[n][j][i])%MOD;
             }
         }
         printf("%d\n",ans);
     }
};
AC A;
int main()
{
    char str[20];
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        if(n+m+k==0) break;
        A.init();
        for(int i=0;i<m;i++)
        {
            scanf("%s",str);
            A.Insert(str,i);
        }
        A.Build_AC();
        A.solve();
    }
    return 0;
}

/*

*/


HDU 2825 Wireless Password(自动机+DP)

标签:

原文地址:http://blog.csdn.net/u013582254/article/details/45159477

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!