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

Wireless Password

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

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
2009 Multi-University Training Contest 1 – Host by TJU  

 

Recommend gaojie       比较综合的题目。AC自动机+DP dp[i][j][k]表示长度为i的串匹配到状态j且字符串中的各个magic word的出现情况为k时的串的个数。i<=25,j<=100,k<=2^10-1。    

  1 //============================================================================
  2 // Name        : HDU.cpp
  3 // Author      : 
  4 // Version     :
  5 // Copyright   : Your copyright notice
  6 // Description : Hello World in C++, Ansi-style
  7 //============================================================================
  8 
  9 #include <iostream>
 10 #include <stdio.h>
 11 #include <string.h>
 12 #include <algorithm>
 13 #include <queue>
 14 using namespace std;
 15 const int MOD=20090717;
 16 int n,m,k;
 17 int dp[30][110][1<<10];
 18 int num[5000];
 19 
 20 struct Trie
 21 {
 22     int next[110][26],fail[110],end[110];
 23     int root,L;
 24     int newnode()
 25     {
 26         for(int i = 0;i < 26;i++)
 27             next[L][i] = -1;
 28         end[L++] = 0;
 29         return L-1;
 30     }
 31     void init()
 32     {
 33         L = 0;
 34         root = newnode();
 35     }
 36     void insert(char buf[],int id)
 37     {
 38         int len = strlen(buf);
 39         int now = root;
 40         for(int i = 0;i < len;i++)
 41         {
 42             if(next[now][buf[i]-'a']==-1)
 43                 next[now][buf[i]-'a'] = newnode();
 44             now = next[now][buf[i]-'a'];
 45         }
 46         end[now] |= (1<<id);
 47     }
 48     void build()
 49     {
 50         queue<int>Q;
 51         fail[root] = root;
 52         for(int i = 0;i < 26;i++)
 53             if(next[root][i] == -1)
 54                 next[root][i] = root;
 55             else
 56             {
 57                 fail[next[root][i]] = root;
 58                 Q.push(next[root][i]);
 59             }
 60         while(!Q.empty())
 61         {
 62             int now = Q.front();
 63             Q.pop();
 64             end[now] |= end[fail[now]];
 65             for(int i = 0;i < 26;i++)
 66                 if(next[now][i] == -1)
 67                     next[now][i] = next[fail[now]][i];
 68                 else
 69                 {
 70                     fail[next[now][i]] = next[fail[now]][i];
 71                     Q.push(next[now][i]);
 72                 }
 73         }
 74     }
 75     int solve()
 76     {
 77         //memset(dp,0,sizeof(dp));
 78         for(int i = 0;i <= n;i++)
 79             for(int j = 0;j < L;j++)
 80                 for(int p = 0;p < (1<<m);p++)
 81                     dp[i][j][p]=0;
 82         dp[0][0][0] = 1;
 83         for(int i = 0;i < n;i++)
 84             for(int j = 0;j < L;j++)
 85                 for(int p = 0;p< (1<<m);p++)
 86                     if(dp[i][j][p] > 0)
 87                     {
 88                         for(int x = 0;x < 26;x++)
 89                         {
 90                             int newi = i+1;
 91                             int newj = next[j][x];
 92                             int newp = (p|end[newj]);
 93                             dp[newi][newj][newp] += dp[i][j][p];
 94                             dp[newi][newj][newp]%=MOD;
 95                         }
 96                     }
 97         int ans = 0;
 98         for(int p = 0;p < (1<<m);p++)
 99         {
100             if(num[p] < k)continue;
101             for(int i = 0;i < L;i++)
102             {
103                 ans = (ans + dp[n][i][p])%MOD;
104             }
105         }
106         return ans;
107     }
108 };
109 char buf[20];
110 Trie ac;
111 int main()
112 {
113     for(int i=0;i<(1<<10);i++)
114     {
115         num[i] = 0;
116         for(int j = 0;j < 10;j++)
117             if(i & (1<<j))
118                 num[i]++;
119     }
120     while(scanf("%d%d%d",&n,&m,&k)==3)
121     {
122         if(n== 0 && m==0 &&k==0)break;
123         ac.init();
124         for(int i = 0;i < m;i++)
125         {
126             scanf("%s",buf);
127             ac.insert(buf,i);
128         }
129         ac.build();
130         printf("%d\n",ac.solve());
131     }
132     return 0;
133 }

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/08/08/2627754.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞