HDU2825-Wireless Password
程序员文章站
2022-03-23 11:37:19
...
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.
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
题意:给你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 long
const 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;
}
下一篇: HDU2825
推荐阅读
-
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
-
PHP 加密 Password Hashing API基础知识点
-
ORA-28002: the password will expire within 7 days 解决方法
-
MYSQL安装时解决要输入current root password的解决方法
-
jdbc-mysql数据库的PASSWORD函数在数据库的表中设置密码有什么限制?
-
MySql5.6使用validate password 插件加强密码强度的安装及使用方法
-
MYSQL安装时解决要输入current root password的解决方法
-
MYSQL安装时解决要输入current root password的解决方法
-
MySQL 5.7.16 修改密码提示 ERROR 1054 (42S22): Unknown column 'password' in 'field list'的原因
-
MYSQL ERROR 1045 (28000): Access denied for user (using password: YES)问题的解决