POJ1699 Best Sequence(AC自动机+状压DP)
题目,求包含所有的给定的n个DNA片段的序列的最短长度。
AC自动机上的DP题。
- dp[S][u]表示已经包含的DNA片段集合为S,且当前后缀状态是自动机第u个结点的最短长度
- dp[0][0]=0
- 我为人人+队列轻松转移。。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define INF (1<<30)
int tn,ch[][],fail[],flag[];
int idx[];
void insert(char *s,int k){
int x=;
for(int i=; s[i]; ++i){
int y=idx[s[i]];
if(ch[x][y]==) ch[x][y]=++tn;
x=ch[x][y];
}
flag[x]|=(<<k);
}
void getfail(){
memset(fail,,sizeof(fail));
queue<int> que;
for(int i=; i<; ++i){
if(ch[][i]) que.push(ch[][i]);
}
while(!que.empty()){
int x=que.front(); que.pop();
for(int i=; i<; ++i){
if(ch[x][i]){
que.push(ch[x][i]);
fail[ch[x][i]]=ch[fail[x]][i];
flag[ch[x][i]]|=flag[ch[fail[x]][i]];
}else ch[x][i]=ch[fail[x]][i];
}
}
}
struct Node{
int s,x;
Node(int _s,int _x):s(_s),x(_x){}
};
int d[<<][],vis[<<][];
int main(){
idx['A']=; idx['C']=; idx['G']=; idx['T']=;
int t,n;
char str[];
scanf("%d",&t);
while(t--){
tn=;
memset(ch,,sizeof(ch));
memset(flag,,sizeof(flag));
scanf("%d",&n);
for(int i=; i<n; ++i){
scanf("%s",str);
insert(str,i);
}
getfail();
for(int i=; i<(<<n); ++i){
for(int j=; j<=tn; ++j) d[i][j]=INF;
}
d[][]=;
memset(vis,,sizeof(vis));
vis[][]=;
queue<Node> que;
que.push(Node(,));
while(!que.empty()){
int s=que.front().s,x=que.front().x; que.pop();
for(int i=; i<; ++i){
int ns=s|flag[ch[x][i]],nx=ch[x][i];
if(d[ns][nx]>d[s][x]+){
d[ns][nx]=d[s][x]+;
if(!vis[ns][nx]){
vis[ns][nx]=;
que.push(Node(ns,nx));
}
}
}
vis[s][x]=;
}
int res=INF;
for(int i=; i<=tn; ++i){
res=min(res,d[(<<n)-][i]);
}
printf("%d\n",res);
}
return ;
}
POJ1699 Best Sequence(AC自动机+状压DP)的更多相关文章
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】
题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...
- HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解
题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...
- zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)
Time Limit: 10 Seconds Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...
- hdu2825 Wireless Password(AC自动机+状压dp)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- 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 6086 -- Rikka with String(AC自动机 + 状压DP)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- UVALive - 4126 Password Suspects (AC自动机+状压dp)
给你m个字符串,让你构造一个字符串,包含所有的m个子串,问有多少种构造方法.如果答案不超过42,则按字典序输出所有可行解. 由于m很小,所以可以考虑状压. 首先对全部m个子串构造出AC自动机,每个节点 ...
- 【hdu2825】ac自动机 + 状压dp
传送门 题目大意: 给你一些密码片段字符串,让你求长度为n,且至少包含k个不同密码片段串的字符串的数量. 题解: 因为密码串不多,可以考虑状态压缩 设dp[i][j][sta]表示长为i的字符串匹配到 ...
随机推荐
- 第16章 使用Squid部署代理缓存服务
章节概述: 本章节从代理缓存服务的工作原理开始讲起,让读者能够清晰理解正向代理(普通模式.透明模式)与反向代理的作用. 正确的使用Squid服务程序部署代理缓存服务可以有效提升访问静态资源的效率,降低 ...
- unity3d进行脚本资源打包加载
原地址:http://www.cnblogs.com/hisiqi/p/3204752.html 本文记录如何通过unity3d进行脚本资源打包加载 1.创建TestDll.cs文件 public c ...
- UITableview reloadData Animation 动画效果
http://blog.kingiol.com/blog/2013/10/22/uitableview-reloaddata-with-animation/ 运用到UITableview进行重新加载数 ...
- nginx(五)nginx与php的安装配置
经过前面学习,对nginx有个大概的了解,来配置LNMP;只要是在系统安装过程中选择安装比较齐全的包,基本上系统都能满足安装要求,下面是我一个一个测试的,基本上全部安装所需的库文件,放心安装: [ro ...
- cmake编译dcmtk,并利用vs2010 进行开发mfc 程序
这几天要处理 医学图像数据,经同学推荐 采用 dcmtk 关于 编译 dcmtk 是可参考如下blog 1. http://blog.csdn.net/okaimee/article/details/ ...
- [ruby on rails] 跟我学之(3)基于rails console的查增删改操作
本章节展开对model的介绍:包括查增删改操作.紧接着上面一节<[ruby on rails] 跟我学之HelloWorld> 创建模型 使用命令创建模型 创建表post,默认自带两栏位 ...
- centos安装ssdb
在编译之前要下gcc编译器 yum -y install gcc* 编译和安装 wget --no-check-certificate https://github.com/ideawu/ssdb ...
- firefox30浏览器,在使用quit()方法退出时,plugin-container.exe崩溃的问题
如题,崩溃截图如下: 解决办法: 对于版本号大于29的firefox,需要在其安装目录下,删除plugin-container.exe,不然使用webdriver的quit()方法关闭浏览器时会报错. ...
- (原创)Python字符串系列(1)——str对象
在本博客 <Python字符串系列> 中,将介绍以下内容: Python内置的str对象及操作 字符串的格式化 Python中的正则表达式 re模块 本文将介绍Python内置的 str ...
- Java for LeetCode 049 Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...