[HDU2825]Wireless Password-AC自动机

#Wireless Password

###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


新道具取得:MLE自动机 X2…

(╯‵□′)╯︵┻━┻


思路:
先建好AC自动机~
然后,根据题意咱是要找到长为n的含有k个膜法魔法单词的的串有多少种…
注意到 M < = 10 M<=10 M<=10,显然钦定状压DP~
d p [ i ] [ j ] [ k ] dp[i][j][k] dp[i][j][k]表示长i个字符、当前在自动机j节点、状态为k的串个数…
显然枚举到了end就转移到一个新的k,否则k相同~
最后统计dp[n][j][bitcount>=k]的状态的值的和即可~

最后咱用了一个黑科技:__builtin_popcount(),作用是返回传入数字二进制意义下1的个数~
%%%教咱这个黑科技的毛爷爷~~~

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

using namespace std;

const int N=1011,md=20090717;

int n,m,kk;

inline int read()
{
	int x=0;char ch=getchar();
	while(ch<'0' || '9'<ch)ch=getchar();
	while('0'<=ch && ch<='9')
	{
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x;
}

struct AC_Automaton
{
	int next[N][26],fail[N];
	int pool,q[N],l,r;
	int dp[26][110][1<<10];
	int end[N];
	
	void init()
	{
		pool=1;
		memset(next,0,sizeof(next));
		memset(fail,0,sizeof(fail));
		memset(end,0,sizeof(end));
		memset(dp,0,sizeof(dp));
	}

	void add(char *s,int id)
	{
		int now=0;
		int len=strlen(s);
		for(int i=0;i<len;i++)
		{
			if(!next[now][s[i]-'a'])
				next[now][s[i]-'a']=++pool;
			now=next[now][s[i]-'a'];
		}
		end[now]|=id;
	}

	void getfail()
	{
		q[r=1]=0;l=0;
		fail[0]=-1;

		while(l!=r)
		{
			int u=q[++l];
			for(int i=0;i<26;i++)
			{
				if(next[u][i])
				{
					q[++r]=next[u][i];
					fail[next[u][i]]= u==0?0:next[fail[u]][i];
				}
				else
					next[u][i]= u==0?0:next[fail[u]][i];
			}
			end[u]|=end[fail[u]];
		}
	}

	long long calc()
	{
		int maxx=(1<<m);
		for(int i=0;i<=n;i++)
			for(int j=0;j<=pool;j++)
				for(int k=0;k<maxx;k++)
					dp[i][j][k]=0;

		dp[0][0][0]=1;

		for(int i=0;i<n;i++)
			for(int j=0;j<=pool;j++)
				for(int k=0;k<maxx;k++)
					if(dp[i][j][k])
						for(int l=0;l<26;l++)
							(dp[i+1][next[j][l]][k|end[next[j][l]]]+=dp[i][j][k])%=md;

		long long ans=0;

		for(int i=0;i<=pool;i++)
		{
			for(int j=0;j<maxx;j++)
			{
				if(__builtin_popcount(j)>=kk)
				{
					(ans+=dp[n][i][j])%=md;
				}
			}
		}

		return ans;
	}
}koishi;

int main()
{
	char s[30];
	while(((n=read())|(m=read())|(kk=read())))
	{
		koishi.init();
		for(int i=1;i<=m;i++)
		{
			scanf("%s",s);
			koishi.add(s,1<<i-1);
		}

		koishi.getfail();
		printf("%lld\n",koishi.calc());
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值