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): 6710    Accepted Submission(s): 2227


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

题意:给你m个字符串,设计一个长为n的密码,使得这个密码中至少含有k个所给的的字符串,问你能设计多少种。

题解:显然是DP了,定义dp[i][j][k]:表示前i位以结点j结尾(匹配到结点j)且当前所含的已知字符串状态为k的方案数。

(PS:题目不难,贵在熟练和加深理解)

#include<map>      
#include<stack>      
#include<queue>      
#include<vector>      
#include<math.h>      
#include<stdio.h>      
#include<iostream>      
#include<string.h>      
#include<stdlib.h>      
#include<algorithm>      
using namespace std;      
typedef long long  ll;      
#define inf 1000000000     
#define mod 20090717       
#define  maxn  650    
#define  lowbit(x) (x&-x)      
#define  eps 1e-10  
int pre[maxn],a[maxn][30],flag[maxn],size,n,m,k,dp[30][110][1<<10],sum[5005];
char s1[30];
queue<int>q;
void init()
{
	int i,j;
	for(i=0;i<(1<<10);i++)
	{
		sum[i]=0;
		for(j=0;j<10;j++)
			if(i&(1<<j))
				sum[i]++;
	}
}
void insert(int id)
{
    int i,len=strlen(s1),now=0,cnt=0;
    for(i=0;i<len;i++)
    {
        int v=s1[i]-'a';
        if(!a[now][v])
		{
			flag[size]=0;
			memset(a[size],0,sizeof(a[size]));
            a[now][v]=size++;
		}
		now=a[now][v];
    }
	flag[now]|=(1<<id);
}
void build_fail()
{
    int now,i;
	for(i=0;i<26;i++)
	{
		int tmp=a[0][i];
		if(tmp)
			pre[tmp]=0,q.push(tmp);
	}
	while(q.empty()==0)
    {
		now=q.front();
		q.pop();
		if(flag[pre[now]])
			flag[now]|=flag[pre[now]];
        for(i=0;i<26;i++)
        {
            if(a[now][i]==0)
			{
				a[now][i]=a[pre[now]][i];
                continue;
			}
			pre[a[now][i]]=a[pre[now]][i];
			q.push(a[now][i]);
        }   
    }
}  
void work()
{
	int i,j,h,num;
	for(i=0;i<=n;i++)
		for(j=0;j<size;j++)
			for(num=0;num<(1<<m);num++)
				dp[i][j][num]=0;
	dp[0][0][0]=1;
	for(i=0;i<n;i++)
		for(j=0;j<size;j++)
			for(num=0;num<(1<<m);num++)
			{
				if(dp[i][j][num]==0)
					continue;
				for(h=0;h<26;h++)
					dp[i+1][a[j][h]][(num|flag[a[j][h]])]=(dp[i+1][a[j][h]][(num|flag[a[j][h]])]+dp[i][j][num])%mod;
			}
	int ans=0;
	for(i=0;i<(1<<m);i++)
	{
		if(sum[i]<k)
			continue;
		for(j=0;j<size;j++)
			ans=(ans+dp[n][j][i])%mod;
	}
	printf("%d\n",ans);
}
int  main(void)
{
    int i;init();
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
	{
		if(n==m && m==k && k==0)
			break;
		size=1;pre[0]=0;flag[0]=0;
		memset(a[0],0,sizeof(a[0]));
		memset(pre,0,sizeof(pre));
		for(i=0;i<m;i++)
		{        
			scanf("%s",s1);
			insert(i);
		}
		build_fail();
		work();
		while(q.empty()==0)
			q.pop();
	}
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值