地址:http://acm.hdu.edu.cn/showproblem.php?pid=4057

题目:

Rescue the Rabbit

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2233    Accepted Submission(s): 653

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.

 
Author
HONG, Qize
 
Source
 

思路:

  dp[i][j][s]表示长度为i的串走到第j个节点时状态为s时是否可行。

  如果用dp[i][j][s] 表示长度为i的串走到第j个节点时状态为s时的最小花费会Tle,因为常数会大十倍

  而且需要状压,不然mle。

 #include <queue>
#include <cstring>
#include <cstdio>
using namespace std; const int INF=0x3f3f3f3f;
struct AC_auto
{
const static int LetterSize = ;
const static int TrieSize = LetterSize * ( 1e3 + ); int tot,root,fail[TrieSize],end[TrieSize],next[TrieSize][LetterSize];
bool dp[][TrieSize][<<];
int newnode(void)
{
memset(next[tot],-,sizeof(next[tot]));
end[tot] = ;
return tot++;
} void init(void)
{
tot = ;
root = newnode();
} int getidx(char x)
{
if(x=='A') return ;
else if(x=='C') return ;
else if(x=='G') return ;
return ;
} void insert(char *ss,int x)
{
int len = strlen(ss);
int now = root;
for(int i = ; i < len; i++)
{
int idx = getidx(ss[i]);
if(next[now][idx] == -)
next[now][idx] = newnode();
now = next[now][idx];
}
end[now]|=x;
} void build(void)
{
queue<int>Q;
fail[root] = root;
for(int i = ; i < LetterSize; i++)
if(next[root][i] == -)
next[root][i] = root;
else
fail[next[root][i]] = root,Q.push(next[root][i]);
while(Q.size())
{
int now = Q.front();Q.pop();
for(int i = ; i < LetterSize; i++)
if(next[now][i] == -) next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
end[next[now][i]]|=end[fail[next[now][i]]];
Q.push(next[now][i]);
}
}
} int match(char *ss)
{
int len,now,res;
len = strlen(ss),now = root,res = ;
for(int i = ; i < len; i++)
{
int idx = getidx(ss[i]);
int tmp = now = next[now][idx];
while(tmp)
{
res += end[tmp];
end[tmp] = ;//°´ÌâÄ¿ÐÞ¸Ä
tmp = fail[tmp];
}
}
return res;
} int go(int n,int m,int *v)
{
int ans=-0x3f3f3f3f,now=,pre=;
memset(dp[pre],,sizeof dp[pre]);
dp[][][]=;
for(int i=,mx=<<m;i<n;i++)
{
memset(dp[now],,sizeof dp[now]);
for(int j=;j<tot;j++)
for(int s=;s<mx;s++)
for(int k=;dp[pre][j][s]&&k<LetterSize;k++)
dp[now][next[j][k]][s|end[next[j][k]]]=;
swap(now,pre);
}
for(int i=,mx=<<m;i<tot;i++)
for(int j=;j<mx;j++)
if(dp[pre][i][j])
{
int tmp=;
for(int k=;k<m;k++)
if(j&(<<k))
tmp+=v[k];
ans=max(ans,tmp);
}
return ans;
}
void debug()
{
for(int i = ;i < tot;i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
for(int j = ;j < LetterSize;j++)
printf("%3d",next[i][j]);
printf("]\n");
}
}
};
AC_auto ac;
char ss[];
int v[];
int main(void)
{
//freopen("in.acm","r",stdin);
int n,m;
while(~scanf("%d%d",&m,&n))
{
ac.init();
for(int i=;i<m;i++)
scanf("%s%d",ss,v+i),ac.insert(ss,<<i);
ac.build();
int ans=ac.go(n,m,v);
if(ans<) printf("No Rabbit after 2012!\n");
else printf("%d\n",ans);
}
return ;
}

hdu4057 Rescue the Rabbit的更多相关文章

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

    Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. hdu4057 Rescue the Rabbit(AC自己主动机+DP)

    Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU4057 Rescue the Rabbit(AC自动机+状压DP)

    题目大概是给几个DNA片段以及它们各自的权值,如果一个DNA包含某个片段那么它的价值就加上这个片段的权值,同时包含多个相同DNA片段也只加一次,问长度l的DNA可能的最大价值. 与HDU2825大同小 ...

  4. JSOI2009 密码 和 JSOI2007 文本生成器 和 ZOJ3545 Rescue the Rabbit

    密码 众所周知,密码在信息领域起到了不可估量的作用.对于普通的登陆口令,唯一的破解 方法就是暴力破解一逐个尝试所有可能的字母组合,但这是一项很耗时又容易被发现的工 作.所以,为了获取对方的登陆口令,在 ...

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

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

  6. ZOJ 3545 Rescue the Rabbit(AC自动机+状压DP)(The 2011 ACM-ICPC Asia Dalian Regional Contest)

    Dr. X is a biologist, who likes rabbits very much and can do everything for them. 2012 is coming, an ...

  7. 【HDOJ】4057 Rescue the Rabbit

    挺有意思的一道题目,解法是AC自动机+DP.AC自动机建立fail指针时,一定要注意结点的属性也需要传递.AC自动机结合了trie和kmp的优点.需要注意的是,每个模式串仅计算一次,否则这题很难解. ...

  8. Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)

    标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...

  9. ZOJ3545 Rescue the Rabbit

    分析 未知定长串中不同已知模板串的出现次数问题,一般做法是AC自动机上dp. 考虑背包,\(dp(i,j,k)\)表示当前串长为\(i\),在AC自动机上对应节点\(j\),已匹配的模板串的状态为\( ...

随机推荐

  1. UNIQLO

    UNIQLO品牌的迅销公司建立于1963年,当年是一家销售西服的小服装店.公司现任董事长兼总经理柳井正早年毕业于早稻田大学经济学专业,1972年8月进入迅销公司,1984年9月就任公司董事长兼总经理. ...

  2. UVa 11178:Morley’s Theorem(两射线交点)

    Problem DMorley’s TheoremInput: Standard Input Output: Standard Output Morley’s theorem states that ...

  3. NPOI例子

    例子链接:http://www.cnblogs.com/atao/tag/NPOI/default.html?page=1

  4. FCN 分割网络详解

    博客来源于:https://www.cnblogs.com/gujianhan/p/6030639.html: https://blog.csdn.net/sinat_24143931/article ...

  5. 系统管理模块_部门管理_改进_抽取添加与修改JSP页面中的公共代码_在显示层抽取BaseAction_合并Service层与Dao层

    系统管理模块_部门管理_改进1:抽取添加与修改JSP页面中的公共代码 commons.jspf <%@ page language="java" import="j ...

  6. 【BZOJ2783】[JLOI2012]树 DFS+栈+队列

    [BZOJ2783][JLOI2012]树 Description 在这个问题中,给定一个值S和一棵树.在树的每个节点有一个正整数,问有多少条路径的节点总和达到S.路径中节点的深度必须是升序的.假设节 ...

  7. if...else if...else和switch语句的注意点

    java if...else if...else 语句 if 语句后面可以跟 elseif…else 语句,这种语句可以检测到多种可能的情况. 使用 if,else if,else 语句的时候,需要注 ...

  8. java的list集合如何根据对象中的某个字段排序?

    转自:http://blog.csdn.net/wangjuan_01/article/details/51351633 List集合按某个字段排序 package wjtest_01; import ...

  9. PHP mysql 扩展库 操作mysql数据库步骤

    一.mysql 扩展库操作mysql数据库步骤如下: 1.获取连接 2.选择数据库 3.设置操作编码 4.发送指令(ddl数据定义/dml数据操作/dql数据查询/dtl数据事务控制) 5.接收返回的 ...

  10. div img span 垂直居中问题

    div设置height,line-height影响块元素内的文本垂直居中,块下的a,img,span等行内元素是没能垂直居中,用了vertical-align:middle;可以处理div等块级元素下 ...