题目链接

Problem Description
Dr. X is a biologist, who likes rabbits very much and can do everything for them. 2012 is coming, and Dr. X wants to take some rabbits to Noah's Ark, or there are no rabbits any more.

A rabbit's genes can be expressed as a string whose length is l (1 ≤ l ≤ 100) containing only 'A', 'G', 'T', 'C'. There is no doubt that Dr. X had a in-depth research on the rabbits' genes. He found that if a rabbit gene contained a particular gene segment, we could consider it as a good rabbit, or sometimes a bad rabbit. And we use a value W to measure this index.

We can make a example, if a rabbit has gene segment "ATG", its W would plus 4; and if has gene segment "TGC", its W plus -3. So if a rabbit's gene string is "ATGC", its W is 1 due to ATGC contains both "ATG"(+4) and "TGC"(-3). And if another rabbit's gene string is "ATGATG", its W is 4 due to one gene segment can be calculate only once.

Because there are enough rabbits on Earth before 2012, so we can assume we can get any genes with different structure. Now Dr. X want to find a rabbit whose gene has highest W value. There are so many different genes with length l, and Dr. X is not good at programming, can you help him to figure out the W value of the best rabbit.

 
Input
There are multiple test cases. For each case the first line is two integers n (1 ≤ n ≤ 10),l (1 ≤ l ≤ 100), indicating the number of the particular gene segment and the length of rabbits' genes.

The next n lines each line contains a string DNAi and an integer wi (|wi| ≤ 100), indicating this gene segment and the value it can contribute to a rabbit's W.

 
Output
For each test case, output an integer indicating the W value of the best rabbit. If we found this value is negative, you should output "No Rabbit after 2012!".
 
Sample Input
2 4
ATG 4
TGC -3
1 6
TGC 4
4 1
A -1
T -2
G -3
C -4
 
Sample Output
4
4
No Rabbit after 2012!

 
Hint

case 1:we can find a rabbit whose gene string is ATGG(4), or ATGA(4) etc.

case 2:we can find a rabbit whose gene string is TGCTGC(4), or TGCCCC(4) etc.
case 3:any gene string whose length is 1 has a negative W.
 
题意:现在有n个基因片段(用包含A、G、T、C的字符串表示),每个基因片段有一个权值,现在求长为L的基因的最大权值(每个基因片段重复出现算一次,不用计算多次)?
 
思路:AC自动机+状态压缩DP,dp[i][j][s]表示长为 i 的基因且在AC自动机 tire树上匹配到节点 j 时包含基因片段集合为 s 时合法(即dp[i][j][s]=1,如果等于0 则表示不存在这样的基因),那么由长为 i 的字符串可以递推到长为 i+1 的字符串基因, x=node[j].son[k]->id 和  f=node[j].son[k]->flag => dp[i+1][x][s|f]=1  (0<=k<=3), 最后计算长为L的基因最大权值和,根据dp[L][j][s] 遍历所有节点的所有状态,计算每个状态下的权值和,得到最大权值和。
 
注意:dp每次根据前一长度下的状态 推 下一长度下的状态,那么可以使用滚动数组减小存储空间。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
using namespace std;
const int N=;
int a[],tot;
struct Node
{
Node *fail;
Node *son[];
int flag;
int id;
}node[N],*root;
queue<Node*>Q;
bool dp[][N][]; int f(char c)
{
if(c=='A') return ;
if(c=='G') return ;
if(c=='T') return ;
if(c=='C') return ;
}
void insert(string s,int id)
{
Node *now=root;
for(int i=;i<s.length();i++)
{
int x=f(s[i]);
if(now->son[x]==NULL) now->son[x]=&node[tot++];
now=now->son[x];
}
now->flag|=(<<(id-));
}
void build()
{
Q.push(root);
while(!Q.empty())
{
Node *now=Q.front(); Q.pop();
for(int i=;i<;i++)
{
if(now->son[i])
{
Node *p=now->fail;
while(p&&(!(p->son[i]))) p=p->fail;
now->son[i]->fail=(p)?(p->son[i]):root;
now->son[i]->flag|=now->son[i]->fail->flag;
Q.push(now->son[i]);
}
else now->son[i]=(now!=root)?now->fail->son[i]:root;
}
}
}
void init()
{
tot=;
root=&node[];
memset(dp,,sizeof(dp));
while(!Q.empty()) Q.pop();
for(int i=;i<N;i++)
{
node[i].fail=NULL;
node[i].flag=;
node[i].id=i;
for(int j=;j<;j++) node[i].son[j]=NULL;
}
}
int main()
{
int n,l;
while(scanf("%d%d",&n,&l)!=EOF)
{
init();
for(int i=;i<=n;i++)
{
string s; cin>>s;
insert(s,i);
scanf("%d",&a[i]);
}
build();
dp[][][]=;
int cn=;
for(int i=;i<l;i++)
{
cn^=;
memset(dp[cn],,sizeof(dp[cn]));
for(int j=;j<tot;j++)
{
for(int s=;s<(<<n);s++)
{
if(!dp[cn^][j][s]) continue;
for(int k=;k<;k++)
{
int x=node[j].son[k]->id;
int f=node[j].son[k]->flag;
dp[cn][x][s|f]=;
}
}
}
}
int ans=-;
for(int i=;i<tot;i++)
{
for(int s=;s<(<<n);s++)
{
if(!dp[cn][i][s]) continue;
int tmp=;
int x=s;
for(int k=;k<=n;k++)
{
if(x&) tmp+=a[k];
x>>=;
}
ans=max(ans,tmp);
}
}
if(ans<) puts("No Rabbit after 2012!");
else printf("%d\n",ans);
}
return ;
}

hdu 4057--Rescue the Rabbit(AC自动机+状压DP)的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 2825 Wireless Password(AC自动机 + 状压DP)题解

    题意:m个密码串,问你长度为n的至少含有k个不同密码串的密码有几个 思路:状压一下,在build的时候处理fail的时候要用 | 把所有的后缀都加上. 代码: #include<cmath> ...

  6. HDU 4057 Rescue the Rabbit(AC自动机+DP)

    题目链接 一个数组开小了一点点,一直提示wa,郁闷,这题比上个题简单一点. #include <iostream> #include <cstring> #include &l ...

  7. HDU - 2825 Wireless Password (AC自动机+状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2825 题意:给一些字符串,构造出长度为n的字符串,它至少包含k个所给字符串,求能构造出的个数. 题解: ...

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

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

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

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

随机推荐

  1. poj 1330 LCA最近公共祖先

    今天学LCA,先照一个模板学习代码,给一个离线算法,主要方法是并查集加上递归思想. 再搞,第一个离线算法是比较常用了,基本离线都用这种方法了,复杂度O(n+q).通过递归思想和并查集来寻找最近公共祖先 ...

  2. grunt之connect、watch

    先说下这两个插件配合的用处,简单的说,它们可以拯救你的F5.connect用于建立一个静态服务器,watch监听文件的修改并自动实时刷新浏览器的页面. 还是options走起. connect(V0. ...

  3. 【1414软工助教】团队作业4——第一次项目冲刺(Alpha版本) 得分榜

    题目 团队作业4--第一次项目冲刺(Alpha版本) 作业提交情况情况 所有团队都在规定时间内完成了七次冲刺. 往期成绩 个人作业1:四则运算控制台 结对项目1:GUI 个人作业2:案例分析 结对项目 ...

  4. 201521044091《Java程序设计》第7周学习总结

    1. 本周学习总结 ArrayList代码分析 1.1 解释ArrayList的contains源代码 用于判断Collection中是否包含某个元素.List<T>的contains方法 ...

  5. 2015211230554《Java程序设计》第6周学习总结

    1. 本周学习总结 2. 书面作业 1.1 Object对象中的clone方法是被protected修饰,在自定义的类中覆盖clone方法时需要注意什么? 克隆方法用于创建对象的拷贝,为了使用clon ...

  6. 201521123121 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 对象的封装:将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现 ...

  7. eclipse ide for java ee developers与eclipse ide for java developers有什么区别

    前者集成了WTP,可用于j2ee开发,功能更完善

  8. Sublime使用Ctrl+`作为快捷键弹出Console没有反映的解决办法

    很多Sublime新人都遇到了这个问题,到网上搜,信息很片面,而且不少都是旧版本的.于是有了这篇文章.       默认Sublime使用Ctrl+`作为快捷键弹出Console,但不同的系统抑或安装 ...

  9. 201521123068 《java程序设计》 第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 多线程的冲突:同时运行的线程需要访问共享数据(临界资源) 多线程的互斥访问:两个或两个以上的线程需要同时对同一数据 ...

  10. lintcode.66 二叉树前序遍历

    二叉树的前序遍历    描述 笔记 数据 评测 给出一棵二叉树,返回其节点值的前序遍历. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返 ...