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的字符串匹配到 ...
随机推荐
- 跟着百度学PHP[1]-if条件嵌套
权当自己的学习笔记.望大牛们切勿参考.如若发现错误,万望指出! 慕课任务 假设在发工资的时候,不仅判定性别,还要判定男性是否有房,没有房,可以发放住房补贴,对于女性,判定是否怀孕,怀孕还有怀孕补贴. ...
- linux下软件安装的方法
linux下软件的安装与卸载 第一章 linux下安装软件,如何知道软件安装位置 注:一般的软件的默认安装目录在 jdk-1_6_0_14-linux-i586-rpm.bin ←修改为 ...
- 2015安徽省赛 C.LU的困惑
题目描述 Master LU 非常喜欢数学,现在有个问题:在二维空间上一共有n个点,LU每连接两个点,就会确定一条直线,对应有一个斜率.现在LU把平面内所有点中任意两点连线,得到的斜率放入一个集合中( ...
- Step
php+MySQL html+css JQuery Mobile JavaScript weiPHP Sina Cloud 微信公众订阅号平台开发
- Spring事务传播、隔离等级
事务传播 PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中.这是最常见的选择. PROPAGATION_SUPPORTS 支持当前事 ...
- linux 下如何查看和踢除正在登陆的其它用户 ==>Linux下用于查看系统当前登录用户信息的4种方法
在linux系统中用pkill命令踢出在线登录用户 由于linux服务器允许多用户登录,公司很多人知道密码,工作造成一定的障碍 所以需要有时踢出指定的用户 1/#who 查出当前有那些终端登录(用 ...
- 查看一些特定sql需求的书写
user表,5个人abcde, content表10篇文章,一个人对应两篇文章,有 time字段,查询出五个人的最新文章. select a.id,a.SName,a.ClsNo,a.Scorefr ...
- 【leetcode】Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- 简单的2d图形变换--仿设变换AffineTransform
在ios中常常遇到些小的动画效果,比如点击一个按钮后,按钮上的三角形图片就旋转了.这种简单的小动画,常常通过更改view的transform属性来实现.这个transform属性,就是一个仿射变化矩阵 ...
- Java for LeetCode 068 Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters ...