hdu4057 Rescue the Rabbit
地址: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
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.
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.
ATG 4
TGC -3
1 6
TGC 4
4 1
A -1
T -2
G -3
C -4
4
No Rabbit after 2012!
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.
思路:
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的更多相关文章
- HDU-4057 Rescue the Rabbit(AC自动机+DP)
Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu4057 Rescue the Rabbit(AC自己主动机+DP)
Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU4057 Rescue the Rabbit(AC自动机+状压DP)
题目大概是给几个DNA片段以及它们各自的权值,如果一个DNA包含某个片段那么它的价值就加上这个片段的权值,同时包含多个相同DNA片段也只加一次,问长度l的DNA可能的最大价值. 与HDU2825大同小 ...
- JSOI2009 密码 和 JSOI2007 文本生成器 和 ZOJ3545 Rescue the Rabbit
密码 众所周知,密码在信息领域起到了不可估量的作用.对于普通的登陆口令,唯一的破解 方法就是暴力破解一逐个尝试所有可能的字母组合,但这是一项很耗时又容易被发现的工 作.所以,为了获取对方的登陆口令,在 ...
- HDU 4057 Rescue the Rabbit(AC自动机+DP)
题目链接 一个数组开小了一点点,一直提示wa,郁闷,这题比上个题简单一点. #include <iostream> #include <cstring> #include &l ...
- 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 ...
- 【HDOJ】4057 Rescue the Rabbit
挺有意思的一道题目,解法是AC自动机+DP.AC自动机建立fail指针时,一定要注意结点的属性也需要传递.AC自动机结合了trie和kmp的优点.需要注意的是,每个模式串仅计算一次,否则这题很难解. ...
- Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)
标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...
- ZOJ3545 Rescue the Rabbit
分析 未知定长串中不同已知模板串的出现次数问题,一般做法是AC自动机上dp. 考虑背包,\(dp(i,j,k)\)表示当前串长为\(i\),在AC自动机上对应节点\(j\),已匹配的模板串的状态为\( ...
随机推荐
- 【转】Native Thread for Win32 B-Threads Synchronization(通俗易懂,非常好)
http://www.bogotobogo.com/cplusplus/multithreading_win32B.php Synchronization Between Threads In t ...
- hdu4734(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 思路:记忆化搜索. #include<iostream> #include<c ...
- maven + hessian 简单样例
项目结构例如以下: pom.xml 内容: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&quo ...
- Linq------错误: Unable to determine the principal end of an association between the types
[Table("bma_stores")] public class Store { //加上即可 [Required] public virtual Product Produc ...
- Java Tomcat 启动闪屏-原因之一---配置问题
如Tomcat启动异常,首先确保Java安装和Tomcat安装版本是否对应,环境变量是否配置正确,如检查通过后,依然启动闪屏.可以依次解决: 1.在Tomcat启动文件Startup.bat之中最后添 ...
- NData转化
NSdata 与 NSString,Byte数组,UIImage 的相互转换---ios开发 Objective-C 1. NSData 与 NSStringNSData-> NSStringN ...
- 第1章 部署虚拟环境安装linux系统
章节简述: 本章节带领读者从0基础了解虚拟机软件与红帽系统,完整的演示了在VM与KVM中安装红帽RHEL7系统的方法. 特别增加了超级实用的Linux系统找回root密码.虚拟机功能增强包.VNC远程 ...
- Xamarin绑定微信SDK 实现分享功能
从开始做这一块的工作开始,就开始找各种的资料,最后还是老老实实的去看官方文档. 对于Xamarin.Android的绑定属于纯jar的绑定,这个难度较小,添加Bindings Library,将lib ...
- jenkins提交SVN文件
需求背景: 公司有内网和外网两台SVN服务器,都需要维护相同的配置文件,但是我们想能否在内网修改配置文件后同时提交到外网SVN服务器上. 开发人员操作步骤 1.开发人员在IDE中checkout内网c ...
- 关于Springboot 中注入多个cacheManage 时候 存在报错
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'e ...