您的位置:首页 > 其它

HDU 2825 Wireless Password(AC自动机+状态压缩DP)

2012-08-08 10:16 489 查看

Wireless Password

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2189 Accepted Submission(s): 610


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, please output the number of possible passwords MOD 20090717.

[align=left]Sample Input[/align]

10 2 2 hello world 4 1 1 icpc 10 0 0 0 0 0

[align=left]Sample Output[/align]

2 1 14195065

[align=left]Source[/align]
2009 Multi-University Training Contest 1 - Host by TJU

[align=left]Recommend[/align]
gaojie

比较综合的题目。AC自动机+DP
dp[i][j][k]表示长度为i的串匹配到状态j且字符串中的各个magic word的出现情况为k时的串的个数。i<=25,j<=100,k<=2^10-1。

//============================================================================
// Name        : HDU.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
const int MOD=20090717;
int n,m,k;
int dp[30][110][1<<10];
int num[5000];

struct Trie
{
int next[110][26],fail[110],end[110];
int root,L;
int newnode()
{
for(int i = 0;i < 26;i++)
next[L][i] = -1;
end[L++] = 0;
return L-1;
}
void init()
{
L = 0;
root = newnode();
}
void insert(char buf[],int id)
{
int len = strlen(buf);
int now = root;
for(int i = 0;i < len;i++)
{
if(next[now][buf[i]-'a']==-1)
next[now][buf[i]-'a'] = newnode();
now = next[now][buf[i]-'a'];
}
end[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();
end[now] |= end[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]);
}
}
}
int solve()
{
//memset(dp,0,sizeof(dp));
for(int i = 0;i <= n;i++)
for(int j = 0;j < L;j++)
for(int p = 0;p < (1<<m);p++)
dp[i][j][p]=0;
dp[0][0][0] = 1;
for(int i = 0;i < n;i++)
for(int j = 0;j < L;j++)
for(int p = 0;p< (1<<m);p++)
if(dp[i][j][p] > 0)
{
for(int x = 0;x < 26;x++)
{
int newi = i+1;
int newj = next[j][x];
int newp = (p|end[newj]);
dp[newi][newj][newp] += dp[i][j][p];
dp[newi][newj][newp]%=MOD;
}
}
int ans = 0;
for(int p = 0;p < (1<<m);p++)
{
if(num[p] < k)continue;
for(int i = 0;i < L;i++)
{
ans = (ans + dp
[i][p])%MOD;
}
}
return ans;
}
};
char buf[20];
Trie ac;
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)==3)
{
if(n== 0 && m==0 &&k==0)break;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: