HDU2825 Wireless Password ac自動機 + 狀壓DP

一、內容

 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

二、思路

在這裏插入圖片描述

  • 注意當方案數爲0的時候就不要直接算了,直接跳過節約時間。

三、代碼

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 26, M = 105, MOD = 20090717;
int n, m, k, len, f[N][M][1 << 11], tr[M][26], ne[M], fail[M];
char s[N]; 
void add(int id) {
	int p = 0;
	for (int i = 0; s[i]; i++) {
		int j = s[i] - 'a';
		if (!tr[p][j]) tr[p][j] = ++len;
		p = tr[p][j];
	}
	fail[p] = (1 << id); //保存每個字符結尾的狀態 
}
void build() {
	queue<int> q;
	for (int j = 0; j < 26; j++) {
		if (tr[0][j]) q.push(tr[0][j]);
	}
	while (!q.empty()) {
		int p = q.front(); q.pop();
		for (int j = 0; j < 26; j++) {
			int c = tr[p][j];
			if (!c) tr[p][j] = tr[ne[p]][j];
			else {
				ne[c] = tr[ne[p]][j];
				fail[c] |= fail[ne[c]]; //把之前字符的狀態加上 
				q.push(c);
			}
		}
	}
}
int main() {
	while (scanf("%d%d%d", &n, &m, &k), n) {
		memset(tr, 0, sizeof(tr)); len = 0;
		memset(ne, 0, sizeof(ne));
		memset(fail, 0, sizeof(fail));
		for (int i = 0; i < m; i++) {
			scanf("%s", s); add(i);
		}
		build();
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= len; j++) {
				for (int k = 0; k < (1 << m); k++) f[i][j][k] = 0;
			}
		}
		f[0][0][0] = 1;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= len; j++) {
				for (int st = 0; st < 1 << m; st++) {
					if (f[i - 1][j][st] == 0) continue; //減少不必要的計算 
					for (int k = 0; k < 26; k++) {
						int nj = tr[j][k];
						//計算nst 
						int nst = st | fail[nj]; 
						f[i][nj][nst] = (f[i][nj][nst] + f[i - 1][j][st]) % MOD; 
					}
				}
			}
		}
		//統計狀態爲k的
		int ans = 0;
		for (int st = 0; st < 1 << m; st++) {
			int t = 0;
			for (int i = m; i >= 0; i--) {
				if (st & (1 << i)) t++; 
			}	
			if (t >= k) {//至少要k個 
				for (int j = 0; j <= len; j++) {
					ans = (ans + f[n][j][st]) % MOD;
				} 
			} 		
		}
		printf("%d\n", ans); 
	}
	return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章