题目大概是给几个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']
  1. 注意的是要用滚动数组,不然要开上百兆数组。
  2. 用滚动数组要注意初始化。
  3. 转移过程有些计算可以预处理出来,存在数组里,虽然不预处理应该也不会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)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. HDU 4057:Rescue the Rabbit(AC自动机+状压DP)***

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 题意:给出n个子串,串只包含‘A’,'C','G','T'四种字符,你现在需要构造出一个长度为l的串,如果 ...

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

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

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

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

  10. HDU 4057 Rescue the Rabbit ( AC自动机 + 状态压缩DP )

    模板来自notonlysuccess. 模式串只有10个,并且重复出现的分值不累加,因此很容易想到状态压缩. 将模式串加入AC自动机,最多有10*100个状态. dp[i][j][k]:串长为i,在T ...

随机推荐

  1. java笔记--查看和修改线程名称

    查看和修改线程名称 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3893797.html  "谢谢-- java是一种允许 ...

  2. FineUI第六天---表单控件

    表单控件 所有表单控件都有的属性有: ShowLabel:是否显示标签(默认值:true). ShowEmptyLabel:是否显示空白的标签(默认值:false). Label:标签文本(默认值:& ...

  3. [Effective JavaScript 笔记]第18条:理解函数调用、方法调用及构造函数调用之间的不同

    面向对象编程中,函数.方法.类的构造函数是三种不同的概念. JS中,它们只是单个构造对象的三种不同的使用模式. 三种不同的使用模式 函数调用 function hello(username){ ret ...

  4. UNITY3D与iOS交互解决方案

    原地址:http://bbs.18183.com/thread-456979-1-1.html 本帖最后由 啊,将进酒 于 2014-2-27 11:17 编辑 “授人以鱼,不如授人以渔”,以UNIT ...

  5. C++模板常用使用方法介绍

    转载:http://developer.51cto.com/art/201002/182202.htm C++编程语言中的模板应用在一定程度上大大提高了程序开发的效率.我们在这篇文章中为大家详细讲解一 ...

  6. ubuntu14.04 中国源

    deb http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiversedeb http://cn.arc ...

  7. Android 相机对焦模式

    Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE  Camer ...

  8. Java for LeetCode 065 Valid Number

    Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...

  9. July 12th, Week 29th Tuesday, 2016

    When the traveler goes alone he gets acquainted with himself. 独自旅行可以让人更好地了解自己. With other's company, ...

  10. atom 震动特效

    1.下载atom 2.配置环境变量 3.运行apm install activate-power-mode 4.打开Atom激活(control+alt+o(是o不是零)) 注:新标签若没效果可以ct ...