题目,求包含所有的给定的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)的更多相关文章

  1. hdu 2825 aC自动机+状压dp

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

  2. BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】

    题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...

  3. HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解

    题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...

  4. zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)

    Time Limit: 10 Seconds      Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...

  5. hdu2825 Wireless Password(AC自动机+状压dp)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  6. hdu 4057--Rescue the Rabbit(AC自动机+状压DP)

    题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...

  7. hdu 6086 -- Rikka with String(AC自动机 + 状压DP)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  8. UVALive - 4126 Password Suspects (AC自动机+状压dp)

    给你m个字符串,让你构造一个字符串,包含所有的m个子串,问有多少种构造方法.如果答案不超过42,则按字典序输出所有可行解. 由于m很小,所以可以考虑状压. 首先对全部m个子串构造出AC自动机,每个节点 ...

  9. 【hdu2825】ac自动机 + 状压dp

    传送门 题目大意: 给你一些密码片段字符串,让你求长度为n,且至少包含k个不同密码片段串的字符串的数量. 题解: 因为密码串不多,可以考虑状态压缩 设dp[i][j][sta]表示长为i的字符串匹配到 ...

随机推荐

  1. Mysql性能监控

    show processlist; show global variables like 'max_allowed_packet'; // QPS计算(每秒查询数)show global status ...

  2. Activity切换后,如i何保存上一个Activit的状态

    在Activity切换中一般有三种方式保存上一个Activity的状态数据.一.全局变量    public static int type = 0;二.SharedPreference      保 ...

  3. HDU 4858 项目管理(邻接表 暴力模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的! 两个节点间可 ...

  4. Linux守护进程的启动方法

    导读 “守护进程”(daemon)就是一直在后台运行的进程(daemon),通常在系统启动时一同把守护进程启动起来,本文介绍如何将一个 Web 应用,启动为守护进程. 一.问题的由来 Web应用写好后 ...

  5. [Unity3D]计时器/Timer

    原地址:http://blog.sina.com.cn/s/blog_5b6cb9500101aejs.html https://github.com/xuzhiping7/Unity3d-Timer ...

  6. Struts2中通配符的使用

    1.准备工作 新建一个JavaWeb项目HelloWord,导入Struts2的.jar包,在Web.xml下配置Struts2的监听,在src下添加Struts2的配置文件struts.xml:将该 ...

  7. delphi 2007 远程调试

    Remote debugging lets you debug a RAD Studio application running on a remote computer. Once the remo ...

  8. 【OpenStack】OpenStack系列8之Nova详解 Neutron详解

    Neutron下载安装 下载:git clone -b stable/icehouse https://github.com/openstack/neutron.git pip install -r ...

  9. Bulls and Cows

    You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

  10. MySQL下载安装、配置与使用(win7x64)

    用过MySQL之后,不论容量的话,发现比其他两个(sql server .oracle)好用的多,一下子就喜欢上了.下面给那些还不知道怎么弄的童鞋们写下具体的方法步骤. 工具/原料 电脑 win7 6 ...