HDU2825--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
 
#include 
#include 
#include 
using namespace std;
#define MOD 20090717
int dp[26][110][1222];
int val[110],num[1222];
int Cnt,first,rear;
struct node 
{
	node * fail;
	node * next[26];
	int cnt,end;
	node()
	{
		for(int i = 0;i < 26;i++)	next[i] = NULL;
		cnt = Cnt++;
		end = 0;
	}
}*q[112],*qq[112];

void insert(char * s,node * root,int Num)
{
	node * p = root;
	int len = strlen(s);
	for(int i = 0;i < len;i++)
	{
		int id = s[i] - 'a';
		if(p -> next[id] == NULL)
		{
			p -> next[id] = qq[Cnt-1] = new node();
		}
		p = p -> next[id];
	}
	val[p -> cnt] = (1 << Num);
}

void build_ac_automation(node * root)
{
	q[rear++] = root;
	node * p = NULL;
	while(first < rear)
	{
		p = q[first++];
		for(int i = 0;i < 26;i++)
		{
			if(p -> next[i] != NULL)
			{
				if(p == root)
				{
					p -> next[i] -> fail = root;
					val[p -> next[i] -> cnt] |= val[root -> cnt];
				}
				else
				{
					p -> next[i] -> fail = p -> fail -> next[i];
					val[p -> next[i] -> cnt] |= val[p -> fail -> next[i] -> cnt];
				}
				q[rear++] = p -> next[i];
				/*node * temp = p -> next[i];
				while(temp != root && val[temp -> cnt])
				{
					val[p -> next[i] -> cnt] |= val[temp -> cnt];
					temp = temp -> fail;
				}*/
			}
			else
			{
				if(p == root)
				{
					p -> next[i] = root;
					val[p -> next[i] -> cnt] = val[root->cnt];
				}
				else 
				{
					p -> next[i] = p -> fail -> next[i];
					val[p -> next[i] -> cnt] = val[p -> fail -> next[i] -> cnt];
				}
			}
		}
	}
}

char str[18];
int main()
{
	//freopen("in.txt","r",stdin);
	num[0]=0;
	for(int i=1;i<(1<<10);i++) num[i]=num[i>>1] +(i&1);
	int n,m,k;
	while(scanf("%d%d%d",&n,&m,&k)==3 && (n|m|k))
	{
		memset(dp,0,sizeof(dp));
		memset(val,0,sizeof(val));
		first = rear = Cnt = 0;
		node * root = qq[0] = new node();
		for(int i = 0;i < m;i++)
		{
			scanf("%s",str);
			insert(str,root,i);
		}
		build_ac_automation(root);
		dp[0][0][0] = 1;
		int M = 1< next[z] -> cnt;
						dp[i+1][v][k|val[v]] = (dp[i+1][v][k|val[v]] + dp[i][j][k]);
						if(dp[i+1][v][k|val[v]] >= MOD)	dp[i+1][v][k|val[v]] %= MOD;
					}
				}
			}
		}
		int ans = 0;
		for(int i = 0;i < Cnt;i++)
		{
			for(int j = 0;j < M;j++)
			{
				if(num[j] >= k)	ans = (ans + dp[n][i][j]);
				if(ans >= MOD) ans %= MOD;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}



你可能感兴趣的:(字符串_AC自动机)