HDU 2825 Wireless Password (AC自己主动机,DP)
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
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.
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.
10 2 2
hello
world
4 1 1
icpc
10 0 0
0 0 0
2
1
14195065
题意:
给出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)的更多相关文章
- HDU - 2825 Wireless Password(AC自己主动机+DP)
Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...
- hdu 2825 Wireless Password(ac自己主动机&dp)
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Hdu 2457 DNA repair (ac自己主动机+dp)
题目大意: 改动文本串的上的字符,使之不出现上面出现的串.问最少改动多少个. 思路分析: dp[i][j]表示如今 i 个字符改变成了字典树上的 j 节点. 然后顺着自己主动机一直转移方程. 注意合法 ...
- HDU 2825 Wireless Password ( Trie图 && 状态压缩DP )
题意 : 输入n.m.k意思就是给你 m 个模式串,问你构建长度为 n 至少包含 k 个模式串的方案有多少种 分析 : ( 以下题解大多都是在和 POJ 2778 && POJ 162 ...
- HDU 2825 Wireless Password(AC自动机+DP)
题目链接 做题, #include <cstdio> #include <string> #include <cstring> using namespace st ...
- POJ 3691 & 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: ...
- HDU 2896 病毒侵袭 (AC自己主动机)
pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2896 病毒侵袭 AC自己主动机题解
本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...
随机推荐
- nrf52832开发配置文件小记
nrf52832在配置定时器和port事件的时候,需要在nrf_drv_config.h(sdk12.x.0版本)文件中,将相应的使能,比如:#define TIMER0_ENABLED 1否则,是不 ...
- cs229_part5
这部分主要补充一些cs229没涉及到,但是实际上非常重要,而且是实际中真正会用的一些算法,即集成学习. 集成学习 问题背景 既然我们已经知道了很多学习算法,这些算法最终会输出一个结果.能不能把这些结果 ...
- re--模块【转】
为什么要学正则表达式 实际上爬虫一共就四个主要步骤: 明确目标 (要知道你准备在哪个范围或者网站去搜索) 爬 (将所有的网站的内容全部爬下来) 取 (去掉对我们没用处的数据) 处理数据(按照我们想要的 ...
- leetcode刷题——双指针
知识点 专题-B-双指针 题目: 题解: CS-Notes Algorithm_Interview_Notes-Chinese awesome-algorithm zcy19941015的博客
- swift final关键字、?、!可选与非可选符
?符号: 可选型 在初始化时可以赋值为nil !符号: 隐形可选型 类型值不能为nil,如果解包后的可选类型为nil会报运行时错误,主要用在一个变量/常量在定义瞬间完成之后值一定会存在的情况.这主要 ...
- 学习笔记3——WordPress文件目录结构详解
**********根目录********** 1.index.php:WordPress核心索引文件,即博客输出文件.2.license.txt:WordPress GPL许可证文件.3.my-ha ...
- IndiaHacks 2nd Elimination 2017 (unofficial, unrated mirror, ICPC rules)
D. Airplane Arrangements time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 【Luogu】P3052摩天大楼里的奶牛(状压DP)
参见ZHT467的题解. f[i]表示在i这个集合下的最少分组数和当前组最少的容量. 从1到(1<<n)-1枚举i,对于每个i枚举它的子奶牛,然后重载运算符计算. 代码如下 #includ ...
- 计算几何 I. 极角
参考资料 hankcs.com: POJ 1981 Circle and Points 题解 aswmtjdsj: POJ 1981 Circle and Points [定长圆覆盖最多点问题] zx ...
- BZOJ2281 [SDOI2011]黑白棋 【dp + 组合数】
题目 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色不同. 小A可以移动白色棋子 ...