HDU2825AC自动机+状压
//我感觉这题不如叫你不看代码就不知道题干在说啥,强烈吐槽
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.
hello
world
4 1 1
icpc
10 0 0
0 0 0
1
14195065
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,k,dp[30][108][1100],cnt[1100];
const int mod=20090717;
struct AC
{
int ch[108][26],fail[108],val[108],sz,rt;
void init()
{
sz=rt=0;
memset(ch[rt],-1,sizeof(ch[rt]));
}
void insert(char *str,int c)
{
int u=rt,len=strlen(str);
for(int i=0; i<len; ++i)
{
if(ch[u][str[i]-'a']==-1)
{
++sz;
memset(ch[sz],-1,sizeof(ch[sz]));
val[sz]=0;
ch[u][str[i]-'a']=sz;
}
u=ch[u][str[i]-'a'];
}
val[u]=1<<c;
}
void build()
{
queue<int>Q;
int u=rt;
for(int i=0; i<26; ++i)
{
if(ch[u][i]==-1) ch[u][i]=rt;
else
{
fail[ch[u][i]]=rt;
Q.push(ch[u][i]);
}
}
while(!Q.empty())
{
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]);
}
}
}
}
} ac;
char s[55];
int main()
{
for(int i=0; i<1024; ++i) cnt[i]=__builtin_popcount(i);//黑科技
while(scanf("%d%d%d",&n,&m,&k),n||m||k)
{
ac.init();
for(int i=0; i<m; ++i)
{
scanf("%s",s);
ac.insert(s,i);
}
ac.build();
memset(dp,0,sizeof(dp));
dp[0][0][0]=1;
int ed=1<<m;
for(int i=0; i<=n; ++i) for(int j=0; j<=ac.sz; ++j) for(int k=0; k<ed; ++k)
{
if(!dp[i][j][k]) continue;
for(int l=0; l<26; ++l)
{
int u=ac.ch[j][l];
dp[i+1][u][ac.val[u]|k]+=dp[i][j][k];
dp[i+1][u][ac.val[u]|k]%=mod;
}
}
int ans=0;
for(int i=0; i<=ac.sz; ++i) for(int j=0; j<ed; ++j) if(cnt[j]>=k)
ans=(ans+dp[n][i][j])%mod;
printf("%d\n",ans);
}
}
HDU2825AC自动机+状压的更多相关文章
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 6086 -- Rikka with String(AC自动机 + 状压DP)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- [BZOJ1559]密码 AC自动机+状压
问题 K: [JSOI2009]密码 时间限制: 1 Sec 内存限制: 64 MB 题目描述 众所周知,密码在信息领域起到了不可估量的作用.对于普通的登陆口令,唯一的破解 方法就是暴力破解一逐个尝 ...
- bzoj 1212: [HNOI2004]L语言 AC自动机+状压
为什么这道题网上所有题解写的都是N*Len的trie树的暴力啊,4E的复杂度... 为什么暴力还跑这么快啊TAT.. 有一个O(Len)的做法就是先把AC自动机建出来,因为每个字典串的长度很小,所以我 ...
- [HNOI2006]最短母串问题——AC自动机+状压+bfs环形处理
Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 32MB Input 第一行是一个正整数n(n< ...
- BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】
题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...
- UVALive - 4126 Password Suspects (AC自动机+状压dp)
给你m个字符串,让你构造一个字符串,包含所有的m个子串,问有多少种构造方法.如果答案不超过42,则按字典序输出所有可行解. 由于m很小,所以可以考虑状压. 首先对全部m个子串构造出AC自动机,每个节点 ...
- POJ1699【AC自动机+状压DP_感言】
萌新感言: 我的天呐! 因为是AC自动机的专题所以没有管别的...硬着头皮吃那份题解(代码)..[请戳简单美丽可爱的代码(没开玩笑)] 首先讲AC自动机: tag存的是以这个节点为后缀的字符串个数(已 ...
- [HNOI2006]最短母串 (AC自动机+状压)
Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. Input 第一行是一个正整数n(n<=12) ...
- HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解
题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...
随机推荐
- Spring5参考指南:事件Event
文章目录 基于继承的Event 基于注解的Event 异步侦听器 Spring提供了很方便的事件的处理机制,包括事件类ApplicationEvent和事件监听类ApplicationListener ...
- 【抓包工具】tcpdump
tcpdump - dump traffic on a network 根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的“头”完全截获下来提供分析.它支 ...
- includes与indexOf
indexOf(a,b)是在es6之前常用的判断数组/字符串内元素是否存在的api,接收两个参数,第一个a代表要被查找的元素,必填.第二个代表从数组的某个坐标开始查找,可选 在数组中 通过indexO ...
- PHP 面试题总结
1.获取数组最后一个位置的值 比较常规的是:$arr[count($arr)-1]; 貌似还有一个数组函数end();可以直接获取最后一个元素的值.相应的还有reset(),next(),curren ...
- 图论--二分图最佳完美匹配(KM模板)
#include <iostream> #include <cstring> #include <cstdio> using namespace std; cons ...
- P6474 [NOI Online #2 入门组] 荆轲刺秦王
P6474 [NOI Online #2 入门组] 荆轲刺秦王 bfs+差分+卡常 本来我其实是场内选手,但是因为记错提交时间,晚了半小时才交,交不上了,就自动降级为了场外选手 题面复杂,不简述了 首 ...
- unittest 管理用例生成测试报告
# 登录方法的封装 from appium import webdriver from time import sleep from python_selenium.Slide import swip ...
- E. Marbles 状压dp
E. Marbles 这个是一个状压dp 题目大意是:给你一个数组,数组的数在1到20之间,有一个操作就是交换相邻的两个数,问 让所有相同的数相邻的最小操作次数 dp[s] 表示s状态下的操作次数,w ...
- http协议跟tcp协议的简单理解
在说明这两个协议之前,我们先简单说一下网络的分层. 1)应用层 支持网络应用,应用协议仅仅是网络应用的一个组成部分,运行在不同主机上的进程则使用应用层协议进行通信.主要的协议有:http.ftp.te ...
- c#一些常用知识点
UID自动生成随机数 UID.Text = Guid.NewGuid().ToString(); GridView中常用格式化公式 <asp:BoundField DataField=" ...