标签:des style color io os ar for sp div
Description
Input
Output
Sample Input
10 2 2 hello world 4 1 1 icpc 10 0 0 0 0 0
Sample Output
2 1 14195065
题意:让你构造一个长度为n的字符串,使得至少包含共m个的集合里的k个,求个数。
思路:AC自动机构造,需要用到DP的思想,设dp[i][j][k]表示长度为i到达自动机状态为j的时候且状态k时的个数。我们需要用一个来表示状态j包含的字符串的个数,也就是走到字符串末尾的个数,二进制表示。然后还要注意一点的是:我们在构造自动机的时候有fail指针的转移,所以状态j也需要结合它失配时候的状态。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int mod = 20090717;
int n, m, k;
int dp[30][250][1<<10];
int num[5000];
struct Trie {
int nxt[110][26], fail[110], end[110];
int root, sz;
int newNode() {
for (int i = 0; i < 26; i++)
nxt[sz][i] = -1;
end[sz++] = 0;
return sz - 1;
}
void init() {
sz = 0;
root = newNode();
}
void insert(char buf[], int id) {
int now = root;
for (int i = 0; buf[i]; i++) {
if (nxt[now][buf[i]-‘a‘] == -1)
nxt[now][buf[i]-‘a‘] = newNode();
now = nxt[now][buf[i]-‘a‘];
}
end[now] |= (1<<id);
}
void build() {
queue<int> q;
fail[root] = root;
for (int i = 0; i < 26; i++) {
if (nxt[root][i] == -1)
nxt[root][i] = root;
else {
fail[nxt[root][i]] = root;
q.push(nxt[root][i]);
}
}
while (!q.empty()) {
int now = q.front();
q.pop();
end[now] |= end[fail[now]];
for (int i = 0; i < 26; i++) {
if (nxt[now][i] == -1)
nxt[now][i] = nxt[fail[now]][i];
else {
fail[nxt[now][i]] = nxt[fail[now]][i];
q.push(nxt[now][i]);
}
}
}
}
int solve() {
for (int i = 0; i <= n; i++)
for (int j = 0; j < sz; j++)
for (int k = 0; k < (1<<m); k++)
dp[i][j][k] = 0;
dp[0][0][0] = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < sz; j++)
for (int k = 0; k < (1<<m); k++)
if (dp[i][j][k] > 0) {
for (int x = 0; x < 26; x++) {
int ni = i+1;
int nj = nxt[j][x];
int nk = k | end[nj];
dp[ni][nj][nk] += dp[i][j][k];
dp[ni][nj][nk] %= mod;
}
}
int ans = 0;
for (int p = 0; p < (1<<m); p++) {
if (num[p] < k) continue;
for (int i = 0; i < sz; i++)
ans = (ans + dp[n][i][p]) % mod;
}
return ans;
}
} ac;
char buf[20];
int main() {
for (int i = 0; i < (1<<10); i++) {
num[i] = 0;
for (int j = 0; j < 10; j++)
if (i & (1<<j))
num[i]++;
}
while (scanf("%d%d%d", &n, &m, &k) != EOF && n+m+k) {
ac.init();
for (int i = 0; i < m; i++) {
scanf("%s", buf);
ac.insert(buf, i);
}
ac.build();
printf("%d\n", ac.solve());
}
return 0;
}
HDU - 2825 Wireless Password(AC自动机+DP)
标签:des style color io os ar for sp div
原文地址:http://blog.csdn.net/u011345136/article/details/39810343