HDU4057 Rescue the Rabbit(AC自动机+状压DP)
题目大概是给几个DNA片段以及它们各自的权值,如果一个DNA包含某个片段那么它的价值就加上这个片段的权值,同时包含多个相同DNA片段也只加一次,问长度l的DNA可能的最大价值。
与HDU2825大同小异。
- dp[i][j][S]表示长度i(自动机转移i步)、后缀状态为自动机第j个结点、包含的DNA片段为集合S 的DNA最大价值
- dp[0][0][0]=0
- 我为人人转移,从dp[i][j][S]向ATCG四个方向更新dp[i+1][j'][S']
- 注意的是要用滚动数组,不然要开上百兆数组。
- 用滚动数组要注意初始化。
- 转移过程有些计算可以预处理出来,存在数组里,虽然不预处理应该也不会TLE,题目时间给了10000ms。
#include<cstdio>
#include<cstring>
#include<queue>
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 init(){
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];
}
}
}
int d[][][<<],VAL[<<];
int main(){
idx['A']=; idx['G']=; idx['T']=; idx['C']=;
int m,n,a;
char str[];
int val[];
while(~scanf("%d%d",&m,&n)){
tn=;
memset(ch,,sizeof(ch));
memset(flag,,sizeof(flag));
for(int i=; i<m; ++i){
scanf("%s%d",str,val+i);
if(strlen(str)>) continue;
insert(str,i);
}
for(int i=; i<(<<m); ++i){
for(int j=; j<m; ++j){
if((i>>j)&) VAL[i]=VAL[i^(<<j)]+val[j];
}
}
init();
for(int j=; j<=tn; ++j){
for(int k=; k<(<<m); ++k) d[][j][k]=-INF;
}
d[][][]=;
for(int i=; i<n; ++i){
int x=i&;
for(int j=; j<=tn; ++j){
for(int k=; k<(<<m); ++k) d[x^][j][k]=-INF;
}
for(int j=; j<=tn; ++j){
for(int k=; k<(<<m); ++k){
if(d[x][j][k]==-INF) continue;
for(int y=; y<; ++y){
d[x^][ch[j][y]][k|flag[ch[j][y]]]=max(d[x^][ch[j][y]][k|flag[ch[j][y]]],d[x][j][k]+VAL[k^(k|flag[ch[j][y]])]);
}
}
}
}
int res=-INF;
for(int i=; i<=tn; ++i){
for(int j=; j<(<<m); ++j) res=max(res,d[n&][i][j]);
}
if(res<) puts("No Rabbit after 2012!");
else printf("%d\n",res);
}
return ;
}
HDU4057 Rescue the Rabbit(AC自动机+状压DP)的更多相关文章
- zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)
Time Limit: 10 Seconds Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...
- 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 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][ ...
- 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)***
http://acm.hdu.edu.cn/showproblem.php?pid=4057 题意:给出n个子串,串只包含‘A’,'C','G','T'四种字符,你现在需要构造出一个长度为l的串,如果 ...
- 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自动机,每个节点 ...
- HDU 4057 Rescue the Rabbit ( AC自动机 + 状态压缩DP )
模板来自notonlysuccess. 模式串只有10个,并且重复出现的分值不累加,因此很容易想到状态压缩. 将模式串加入AC自动机,最多有10*100个状态. dp[i][j][k]:串长为i,在T ...
随机推荐
- Ubuntu nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
在Ubuntu 12中启动刚安装好的Nginx,报错: nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied) 原因如下: ...
- [BZOJ2423][HAOI2010]最长公共子序列
[BZOJ2423][HAOI2010]最长公共子序列 试题描述 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x ...
- 速度之王 — LZ4压缩算法(三)
LZ4使用 make / make clean 得到可执行程序:lz4.lz4c Usage: ./lz4 [arg] [input] [output] input : a filename Argu ...
- Flip Game I && II
Flip Game I Problem Description: You are playing the following Flip Game with your friend: Given a s ...
- WriteFile实现下载
TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { /* ...
- 关于Xcode6 Segue 的疑问,没有解决!
xcode6 的segue 变化了,如图 关于前3个选项,始终没有太明白,我试验结果如下,简单地把几个viewController连接起来时,无论用show,还是showdetail,还是Presen ...
- codeforces C. Fixing Typos 解题报告
题目链接:http://codeforces.com/problemset/problem/363/C 题目意思:纠正两种类型的typos.第一种为同一个字母连续出现3次以上(包括3次):另一种为两个 ...
- poj 1007 DNA Sorting 解题报告
题目链接:http://poj.org/problem?id=1007 本题属于字符串排序问题.思路很简单,把每行的字符串和该行字符串统计出的字母逆序的总和看成一个结构体.最后把全部行按照这个总和从小 ...
- 【python】在python中调用mysql
资料:http://www.runoob.com/python/python-mysql.html 例子: 假设有数据路HTMLHASH, 其中有表htmlhash, 表中包括两个varchar(10 ...
- [Java基础] SequenceInputStream输入合并流
转载: http://blog.csdn.net/xuefeng1009/article/details/6955707 public SequenceInputStream(Enumeration& ...