HDU-2825 Wireless Password(AC自动机+状压DP)
题目大意:给一系列字符串,用小写字母构造出长度为n的至少包含k个字符串的字符串,求能构造出的个数。
题目分析:在AC自动机上走n步,至少经过k个单词节点,求有多少种走法。
代码如下:
# include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std; typedef long long LL; const int mod=20090717; int ch[105][26];
int cnt,fail[105];
int val[105]; void init()
{
cnt=0;
memset(ch,-1,sizeof(ch));
memset(val,0,sizeof(val));
} int idx(char x)
{
return x-'a';
} void insert(char *s,int k)
{
int len=strlen(s);
int r=0;
for(int i=0;i<len;++i){
int c=idx(s[i]);
if(ch[r][c]==-1) ch[r][c]=++cnt;
r=ch[r][c];
}
val[r]|=(1<<k);
} void getFail()
{
queue<int>q;
fail[0]=0;
for(int i=0;i<26;++i){
if(ch[0][i]==-1)
ch[0][i]=0;
else{
fail[ch[0][i]]=0;
q.push(ch[0][i]);
}
}
while(!q.empty())
{
int u=q.front();
q.pop();
val[u]|=val[fail[u]];
for(int i=0;i<26;++i){
if(ch[u][i]==-1)
ch[u][i]=ch[fail[u]][i];
else{
fail[ch[u][i]]=ch[fail[u]][i];
q.push(ch[u][i]);
}
}
}
} char word[15];
int dp[2][105][1<<10]; int getDigt(int x,int n)
{
int count=0;
for(int i=0;i<n;++i)
if(x&(1<<i)) ++count;
return count;
} int DP(int n,int m,int k)
{
memset(dp,0,sizeof(dp));
int cur=1;
dp[0][0][0]=1;
for(int i=0;i<n;++i){
memset(dp[cur],0,sizeof(dp[cur]));
for(int j=0;j<=cnt;++j){
for(int s=0;s<(1<<m);++s) if(dp[cur^1][j][s]){ ///不加if语句会超时
for(int c=0;c<26;++c){
int &nxt=dp[cur][ch[j][c]][s|val[ch[j][c]]];
nxt=(nxt+dp[cur^1][j][s])%mod;
}
}
}
cur^=1;
}
int ans=0;
for(int i=0;i<(1<<m);++i) if(getDigt(i,m)>=k){
for(int j=0;j<=cnt;++j)
ans=(ans+dp[cur^1][j][i])%mod;
}
return ans;
} int main()
{
int n,m,k;
while(scanf("%d%d%d",&n,&m,&k)&&(n||m||k))
{
init();
for(int i=0;i<m;++i){
scanf("%s",word);
insert(word,i);
}
getFail();
printf("%d\n",DP(n,m,k));
}
return 0;
}
HDU-2825 Wireless Password(AC自动机+状压DP)的更多相关文章
- hdu2825 Wireless Password(AC自动机+状压dp)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- 【HDU2825】Wireless Password (AC自动机+状压DP)
Wireless Password Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u De ...
- HDU2825 Wireless Password —— AC自动机 + 状压DP
题目链接:https://vjudge.net/problem/HDU-2825 Wireless Password Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 4057--Rescue the Rabbit(AC自动机+状压DP)
题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...
- hdu_2825_Wireless Password(AC自动机+状压DP)
题目链接:hdu_2825_Wireless Password 题意: 给你m个串,问长度为n至少含k个串的字符串有多少个 题解: 设dp[i][j][k]表示考虑到长度为i,第j个自动机的节点,含有 ...
- HDU 2825 Wireless Password(AC自动机+DP)
题目链接 做题, #include <cstdio> #include <string> #include <cstring> using namespace st ...
- 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 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 2825 Wireless Password (AC自己主动机,DP)
pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...
随机推荐
- android editText 监听事件
在软键盘中注意 在监听的 edittext中 使用android:imeOptions属性的时候,一定要对EditText设置 android:inputType 或者 设置 android:sing ...
- AngularJs的UI组件ui-Bootstrap分享(十)——Model
Model是用来创建模态窗口的,但是实际上,并没有Model指令,而只有$uibModal服务,创建模态窗口是使用$uibModal.open()方法. 创建模态窗口时,要有一个模态窗口的模板和对应的 ...
- Python的平凡之路(15)
一.CSS补充: 1. 上节课讲述 a.css重用 <style> 如果整个页面的宽度 > 900px时: { ...
- C语言实现粒子群算法(PSO)一
最近在温习C语言,看的书是<C primer Plus>,忽然想起来以前在参加数学建模的时候,用过的一些智能算法,比如遗传算法.粒子群算法.蚁群算法等等.当时是使用MATLAB来实现的,而 ...
- 字节序相关问题简单总结,LSB与MSB
细细碎碎的知识点还真是不少啊,今天总结下通信中的数据字节序的问题. 先来认识名词: MSB:Most Significant Bit. “最高有效位” LSB:Least Significant ...
- 把crosswalk打包到cordova项目中
(1)从Crosswalk官网下载Cordova Android (ARM) 点击下载 (2)解压压缩包到任意目录 (3)创建工程 cordova工程 cordova create Crosswalk ...
- 开始学习Dojo
学习:Dojo入门简易教程 Dojo Toolkit 简介 Dojo 于 2004 年创建,使开发 DHTML 和 JavaScript web 应用程序开发流程更为容易,隐藏了很多现代 web 浏览 ...
- Ubuntu14.04安装build-essential失败,包依赖问题如何解决?
正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 有一些软件包无法被安装.如果您用的是 unstable 发行版,这也许是 因为系统 ...
- RSA_SHA256数字签名
------------------ rsa 加密与解密 ----------------------------- 数论知识的实际应用: rsa加密 把问题归结为对数字的加密. 被加密的数字为明文. ...
- [搬砖]Pycharm中启动IPython notebook失败提示load_entry_point ImportError: Entry point ('console_scripts', 'ipython') not found的解决方法
前提:直接运行ipython正常,“which -a ipython”命令显示也只有一个ipython存在,在ipynb文件中点运行启动notebook时提示错误类似如下: Traceback (mo ...