HDU-2825[AC自动机+状压dp]

不要被状压吓倒了。。和之前用AC自动机求构造字符串的数量方法类似,但是这个问题要多开一维状态表示包含多少特殊的串。
dp[i][j][k]表示长度为i已经匹配到j的串包含特殊串的状态为k的数量。
转移方程是dp[i][u][s|ed[u]] += dp[i-1][j][s](u = j->ch)

#include<bits/stdc++.h>
using namespace std;
char ss[11];
const int mod = 20090717;
struct AC{
	int nex[500][26], root, tot;
	int f[500], ed[500];
	void init() {
		tot = 0;
		root = newnode();
	}
	int newnode() {
		for(int i = 0; i < 26; i++) {
			nex[tot][i] = -1;
		}
		ed[tot] = 0;
		return tot++;
	}
	void insert(char *s, int x) {
		int u = root, len = strlen(s);
		for(int i = 0; i < len; i++) {
			int ch = s[i]-'a';
			if(nex[u][ch] == -1) nex[u][ch] = newnode();
			u = nex[u][ch];
		}
		ed[u] = 1<<(x-1);
	}
	void getfail() {
		queue<int>Q;
		for(int i = 0; i < 26; i++) {
			if(nex[root][i] == -1) nex[root][i] = root;
			else {
				f[nex[root][i]] = root;
				Q.push(nex[root][i]);
			}
		}
		while(!Q.empty()) {
			int u = Q.front();Q.pop();
			ed[u] |= ed[f[u]];
			for(int i = 0; i < 26; i++) {
				if(nex[u][i] == -1) nex[u][i] = nex[f[u]][i];
				else {
					f[nex[u][i]] = nex[f[u]][i];
					Q.push(nex[u][i]);
				}
			}
		}
	}
}ac;
long long dp[30][105][1205];
int cnt[1025];
int main() {
	for(int i = 0; i < 1024; i++) cnt[i] = __builtin_popcount(i);
	int n, m, k;
	while(scanf("%d%d%d", &n, &m, &k) == 3 && (n+m+k)) {
		ac.init();
		for(int i = 1; i <= m; i++) {
			scanf("%s", ss);
			ac.insert(ss, i);
		}
		ac.getfail();
		memset(dp, 0, sizeof(dp));
		dp[0][0][0] = 1;
		int M = 1<<m;
		for(int i = 0; i <= n; i++) {
			for(int j = 0; j < ac.tot; j++) {
				for(int a = 0; a < M; a++) {
					if(!dp[i][j][a]) continue;
					for(int g = 0; g < 26; g++) {
						int t = ac.nex[j][g];
						dp[i+1][t][ac.ed[t]|a] += dp[i][j][a];
						dp[i+1][t][ac.ed[t]|a] %= mod;
					}
				}
			}
		}
		int res = 0;
		for(int i = 0; i < ac.tot; i++) {
			for(int j = 0; j < M; j++) {
				if(cnt[j] >= k) res += dp[n][i][j], res %= mod;
			}
		}
		printf("%d\n", res);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值