HDU2825-Wireless Password

来源:互联网 发布:圆梦会计软件联系方式 编辑:程序博客网 时间:2024/05/22 13:42

Wireless Password

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


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 2hello world 4 1 1icpc 10 0 00 0 0
 

Sample Output
2114195065
 

Source
2009 Multi-University Training Contest 1 - Host by TJU
 

Recommend
gaojie
 

题意:给你m个字符串,问至少用其中的k个字符串构造出一个长度为n的字符串有几种方式

解题思路:dp[i][j][k]表示长度为i匹配到状态j各个字符串出现的情况为k的方案数


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cctype>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;const int mod = 20090717;char ch[15];int n, m, k;int dp[30][200][1 << 10];int vis[1 << 10];struct Trie{int next[200][26], fail[200], flag[200];int root, tot;int newnode(){for (int i = 0; i < 26; i++) next[tot][i] = -1;flag[tot++] = 0;return tot - 1;}void init(){tot = 0;root = newnode();}void insert(char ch[], int id){int k = root;for (int i = 0; ch[i]; i++){if (next[k][ch[i] - 'a'] == -1) next[k][ch[i] - 'a'] = newnode();k = next[k][ch[i] - 'a'];}flag[k] |= (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 pre = q.front();q.pop();flag[pre] |= flag[fail[pre]];for (int i = 0; i < 26; i++){if (next[pre][i] == -1) next[pre][i] = next[fail[pre]][i];else{fail[next[pre][i]] = next[fail[pre]][i];q.push(next[pre][i]);}}}}void solve(){for (int i = 0; i <= n; i++)for (int j = 0; j < tot; j++)for (int p = 0; p < (1 << m); p++)dp[i][j][p] = 0;dp[0][0][0] = 1;for (int i = 0; i < n; i++)for (int j = 0; j < tot; j++)for (int p = 0; p < (1 << m); p++){if (dp[i][j][p]){for (int q = 0; q < 26; q++)(dp[i + 1][next[j][q]][p | flag[next[j][q]]] += dp[i][j][p]) %= mod;}}int ans = 0;for (int i = 0; i < (1 << m); i++){if (vis[i] < k) continue;for (int j = 0; j < tot; j++)(ans += dp[n][j][i]) %= mod;}printf("%d\n", ans);}void debug(){for (int i = 0; i < tot; i++){printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], flag[i]);for (int j = 0; j < 26; j++) printf("%2d", next[i][j]);printf("]\n");}}}ac;int main(){for (int i = 0; i < (1 << 10); i++){for (int j = 0; j < 10; j++)if (i&(1 << j)) vis[i]++;}while (~scanf("%d %d %d", &n, &m, &k) && (n + m + k)){ac.init();for (int i = 0; i < m; i++){scanf("%s", ch);ac.insert(ch, i);}ac.build();ac.solve();}return 0;}