pid=2825">http://acm.hdu.edu.cn/showproblem.php?

pid=2825

Wireless Password
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4560    Accepted Submission(s): 1381
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
10 2 2
hello
world
4 1 1
icpc
10 0 0
0 0 0
 
Sample Output
2
1
14195065
 
Source
 

题意:

给出m个模式串。要求构造一长度为n的文本串,至少包含k种模式串,求有多少种可能的模式串。

分析:

m个模式串构建AC自己主动机。然后要在这AC自己主动机中走n步。至少经过k个单词结点。由于m<=10,显然能够用状压表示已经有哪几个单词结点。用dp[i][j][k]表示走了i步到AC自己主动机中的第j个结点,单词状态为k。由计数原理可推出状态转移方程:dp[i][j][k]=sum(dp[i-1][last_j][last_k]),last_j表示能够抵达第j个结点的上一个结点。last_k表示上一步的状态;由于第i步仅仅和第i-1步有关。所以能够用滚动数组优化空间。

/*
*
* Author : fcbruce <fcbruce8964@gmail.com>
*
* Time : Thu 20 Nov 2014 10:01:45 AM CST
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10 #ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif #define maxm
#define maxn 255 using namespace std; const int mod = 20090717; int dp[2][maxn][1024]; inline int add(int a,int b)
{
return (a+b)%mod;
} int q[maxn]; const int maxsize = 26;
struct ACauto
{
int ch[maxn][maxsize];
int val[maxn],nex[maxn],last[maxn];
int sz; ACauto()
{
memset(ch[0],0,sizeof ch[0]);
val[0]=0;
sz=1;
} void clear()
{
memset(ch[0],0,sizeof ch[0]);
val[0]=0;
sz=1;
} int idx(char c)
{
return c-'a';
} void insert(const char *s,int v)
{
int u=0;
for (int i=0;s[i]!='\0';i++)
{
int c=idx(s[i]);
if (ch[u][c]==0)
{
memset(ch[sz],0,sizeof ch[sz]);
val[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=v;
} void get_fail()
{
int f=0,r=-1;
nex[0]=0;
for (int c=0;c<maxsize;c++)
{
int u=ch[0][c];
if (u!=0)
{
nex[u]=0;
q[++r]=u;
last[u]=0;
}
} while (f<=r)
{
int x=q[f++];
for (int c=0;c<maxsize;c++)
{
int u=ch[x][c];
if (u==0)
{
ch[x][c]=ch[nex[x]][c];
continue;
}
q[++r]=u;
int v=nex[x];
nex[u]=ch[v][c];
val[u]|=val[nex[u]];
}
}
} int calc(int x)
{
int cnt=0;
for (int i=0;i<32;i++)
if (x&(1<<i)) cnt++;
return cnt;
} int DP(int l,int m,int k)
{
memset(dp,0,sizeof dp);
dp[0][0][0]=1;
int x=1;
for (int i=0;i<l;i++,x^=1)
{
memset(dp[x],0,sizeof dp[x]);
for (int j=0;j<sz;j++)
{
for (int s=0;s<m;s++)
{
if (dp[x^1][j][s]==0) continue;
for (int c=0;c<maxsize;c++)
{
int &cur=dp[x][ch[j][c]][s|val[ch[j][c]]];
cur=add(cur,dp[x^1][j][s]);
}
}
}
} int total=0; for (int i=0;i<m;i++)
{
if (calc(i)<k) continue;
for (int j=0;j<sz;j++)
total=add(total,dp[x^1][j][i]);
} return total;
}
}acauto; char str[16]; int main()
{
#ifdef FCBRUCE
freopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCE int n,m,k; while (scanf("%d%d%d",&n,&m,&k),n||m||k)
{
acauto.clear();
for (int i=0;i<m;i++)
{
scanf("%s",str);
acauto.insert(str,1<<i);
} acauto.get_fail(); printf("%d\n",acauto.DP(n,1<<m,k));
} return 0;
}

HDU 2825 Wireless Password (AC自己主动机,DP)的更多相关文章

  1. HDU - 2825 Wireless Password(AC自己主动机+DP)

    Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...

  2. hdu 2825 Wireless Password(ac自己主动机&amp;dp)

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. Hdu 2457 DNA repair (ac自己主动机+dp)

    题目大意: 改动文本串的上的字符,使之不出现上面出现的串.问最少改动多少个. 思路分析: dp[i][j]表示如今 i 个字符改变成了字典树上的 j 节点. 然后顺着自己主动机一直转移方程. 注意合法 ...

  4. HDU 2825 Wireless Password ( Trie图 && 状态压缩DP )

    题意 : 输入n.m.k意思就是给你 m 个模式串,问你构建长度为 n 至少包含 k 个模式串的方案有多少种 分析 : ( 以下题解大多都是在和 POJ 2778 && POJ 162 ...

  5. HDU 2825 Wireless Password(AC自动机+DP)

    题目链接 做题, #include <cstdio> #include <string> #include <cstring> using namespace st ...

  6. POJ 3691 &amp; HDU 2457 DNA repair (AC自己主动机,DP)

    http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...

  7. HDU 2896 病毒侵袭 (AC自己主动机)

    pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...

  8. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  9. HDU 2896 病毒侵袭 AC自己主动机题解

    本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...

随机推荐

  1. 第一章 pyhton基础

    一 .pyhton2与python3的区别 在pyhton2中,其中编码默认使用的是ascii编码,输出格式为print"xxx",输入为raw_input(“请输入”),在整型中 ...

  2. 封装微信分享到朋友/朋友圈js

    在页面引入: <script src="/static/lib/jquery-2.2.2.min.js"></script><script src=& ...

  3. tabel使用总结

    对日常使用到的tabel做下记录: <table cellspacing="0"><!--单元格间距为零 默认为2 border默认为 0--> <t ...

  4. cf950d A Leapfrog in the Array

    考虑在位置 \(p\) 的青蛙. 如果 \(p\) 是奇数,答案显然是 \((p+1)/2\). 否则,由于未跳时 \(p\) 左边有 \(p/2\) 只,则 \(p\) 右边有 \(n-p/2\) ...

  5. Programming Python 3rd Edition 第三版 pdf chm下载

    Programming Python 作为一款经典系列书籍 非常的耐看 建议有志于学习python的童鞋好好看看 网上 Programming Python第四版的 pdf 下载非常容易 也就是最新的 ...

  6. POJ-3352 Road Construction,tarjan缩点求边双连通!

    Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...

  7. redis介绍和安装和主从介绍(二)

    redis正式安装过程 安装依赖,下载解压,编译安装 yum install gcc-c++ tcl wget http://download.redis.io/releases/redis-4.0. ...

  8. shrio注解的方式进行权限控制

    除了通过API方式外,Shiro 提供Java 5+注解的集合,以注解为基础的授权控制.在你可以使用Java 注释之前,你需要在你的应用程序中启用AOP 支持. Shiro注解支持AspectJ.sp ...

  9. 关于流媒体(m3u8)的播放与下载

    前一段时间做了一个视频播放下载应用,抓取的是优酷的视频,虽然优酷有自己的开发平台http://open.youku.com/,但未真正的实现.所以只能靠抓取视频源,Youku的视频采取了加密+动态的获 ...

  10. 【Luogu】P1040加分二叉树(区间DP)

    题目链接 区间DP,因为中序遍历的性质:区间[l,r]的任何一个数都可以是该区间的根节点. 更新权值的时候记录区间的根节点,最后DFS输出. 见代码. #include<cstdio> # ...