Wireless PasswordTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5090 Accepted Submission(s): 1613
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
Sample Output
|
题意:给出m个模式串,并告诉你在长度为n的字符串中包含至少k(0 <= k <= m)个模式串,问长度为n的字符串的总数目。
思路:构建AC自动机,根据Trie状态转移图统计Trie上每个节点对应的状态,并用State[]记录。
把m个模式串压缩成1<<m个状态,用dp[i][S][j];//表示长度为i的串在Trie的S节点包含状态j的总方案数
得到状态转移方程
dp[ i+1 ][ next[S][l] ][ j|State[next[S][l]] ] += dp[i][S][j],其中l为S节点的所有子节点。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXN 105
#define INF 0x3f3f3f3f
#define MOD 20090717
#define LL long long
using namespace std;
int have[1<<10];//当前状态 包含多少个模式串
int n, m, k;
void gethave()
{
for(int i = 0; i < (1<<10); i++)
{
have[i] = 0;
for(int j = 0; j < 10; j++)
{
if(i & (1<<j))
have[i]++;
}
}
}
struct Trie
{
int next[MAXN][30], fail[MAXN], State[MAXN];
int L, root;
int newnode()
{
for(int i = 0; i < 26; i++)
next[L][i] = -1;
State[L++] = 0;
return L-1;
}
void init()
{
L = 0;
root = newnode();
}
void Insert(char *s, int id)
{
int len = strlen(s);
int now = root;
for(int i = 0; i < len; i++)
{
if(next[now][s[i]-'a'] == -1)
next[now][s[i]-'a'] = newnode();
now = next[now][s[i]-'a'];
}
State[now] |= (1<<id);
}
void Build()
{
queue<int> Q;
fail[root] = root;
for(int i = 0; i < 26; i++)
{
if(next[root][i] == -1)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
State[now] |= State[fail[now]];//每个节点所包含模式串状态
for(int i = 0; i < 26; i++)
{
if(next[now][i] == -1)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
//把m个模式串状态压缩成 0——1<<m
LL dp[30][MAXN][1<<10];//表示长度为i的串在Trie的j节点包含状态k的总数
void solve()
{
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for(int i = 0; i < n; i++)
{
for(int S = 0; S < L; S++)
{
for(int j = 0; j < (1<<m); j++)
{
if(dp[i][S][j] > 0)
{
for(int l = 0; l < 26; l++)
{
int nextnode = next[S][l];
int nextState = j | State[nextnode];
dp[i+1][nextnode][nextState] += dp[i][S][j],
dp[i+1][nextnode][nextState] %= MOD;
}
}
}
}
}
LL ans = 0;
for(int i = 0; i < (1<<m); i++)
{
if(have[i] < k) continue;
for(int j = 0; j < L; j++)
ans = (ans + dp[n][j][i]) % MOD;
}
printf("%lld\n", ans);
}
};
Trie ac;
char str[15];
int main()
{
gethave();
while(scanf("%d%d%d", &n, &m, &k), n||m||k)
{
ac.init();
for(int i = 0; i < m; i++)
scanf("%s", str), ac.Insert(str, i);
ac.Build(); ac.solve();
}
return 0;
}