HDU2825-Wireless Password

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 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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值