hdu4057 Rescue the Rabbit(AC自己主动机+DP)
Rescue the Rabbit
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1412 Accepted Submission(s): 403
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.
2 4
ATG 4
TGC -3 1 6
TGC 4 4 1
A -1
T -2
G -3
C -4
4
4
No Rabbit after 2012!Hintcase 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.
pid=4056">
4056
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 1000000000
using namespace std;
int n,l,w;
char s[110];
int L,rt,next[1010][26],end[1010],fail[1010],c[1010];
int dp[2][1010][1<<10];
int newnode(){
memset(next[L],0,sizeof next[L]);
c[L]=0;
end[L++]=0;
return L-1;
}
void init(){
L=0;
rt=newnode();
}
void insert(char *s,int z,int x){
int le=strlen(s),now=rt;
for(int i=0;i<le;i++){
int x=s[i]-'A';
if(!next[now][x]) next[now][x]=newnode();
now=next[now][x];
}
end[now]+=z;
c[now]|=(1<<x);
}
void build(){
queue<int> q;
int x=rt;
for(int i=0;i<26;i++){
if(next[x][i]){
fail[next[x][i]]=rt;
q.push(next[x][i]);
}
}
while(!q.empty()){
x=q.front();
q.pop();
for(int i=0;i<26;i++){
if(!next[x][i]){
next[x][i]=next[fail[x]][i];
}else{
fail[next[x][i]]=next[fail[x]][i];
q.push(next[x][i]);
}
}
}
}
void read(){
for(int i=0;i<n;i++){
scanf("%s %d",s,&w);
insert(s,w,i);
}
}
int f[4]={0,2,6,19};
void solve(){
build();
int cur=0;
for(int j=0;j<L;j++){
for(int k=0;k<(1<<n);k++) dp[cur][j][k]=-inf;
}
dp[cur][0][0]=0;
int ans=-inf;
for(int i=1;i<=l;i++){
for(int j=0;j<L;j++){
for(int k=0;k<(1<<n);k++) dp[1-cur][j][k]=-inf;
}
for(int j=0;j<L;j++){
for(int k=0;k<(1<<n);k++){
if(dp[cur][j][k]==-inf) continue;
for(int p=0;p<4;p++){
int z=f[p],nxt=next[j][z];
int C=0,tmp=0,x=nxt;
while(x){
C|=c[x];
if((k&c[x])==0) tmp+=end[x];
x=fail[x];
}
dp[1-cur][nxt][k|C]=max(dp[1-cur][nxt][k|C],dp[cur][j][k]+tmp);
}
}
}
cur=1-cur;
if(i==l){
for(int j=0;j<L;j++){
for(int k=0;k<(1<<n);k++){
ans=max(ans,dp[cur][j][k]);
}
}
}
}
if(ans<0) puts("No Rabbit after 2012!");
else printf("%d\n",ans);
}
int main(){
while(~scanf("%d%d",&n,&l)){
init();
read();
solve();
}
return 0;
}
hdu4057 Rescue the Rabbit(AC自己主动机+DP)的更多相关文章
- Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)
标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...
- POJ 2778 DNA Sequence (AC自己主动机 + dp)
DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...
- HDU - 2825 Wireless Password(AC自己主动机+DP)
Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...
- Hdu 3341 Lost's revenge (ac+自己主动机dp+hash)
标题效果: 举个很多种DNA弦,每个字符串值值至1.最后,一个长字符串.要安排你最后一次另一个字符串,使其没事子值和最大. IDEAS: 首先easy我们的想法是想搜索的!管她3721..直接一个字符 ...
- hdu4758 Walk Through Squares (AC自己主动机+DP)
Walk Through Squares Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...
- poj 3691 DNA repair(AC自己主动机+dp)
DNA repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5877 Accepted: 2760 Descri ...
- HDU - 4758 Walk Through Squares (AC自己主动机+DP)
Description On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...
- HDU - 4511 小明系列故事――女友的考验(AC自己主动机+DP)
Description 最终放寒假了,小明要和女朋友一起去看电影.这天,女朋友想给小明一个考验,在小明正准备出发的时候.女朋友告诉他.她在电影院等他,小明过来的路线必须满足给定的规则: 1.如果小明 ...
- Codeforces 86C Genetic engineering (AC自己主动机+dp)
题目大意: 要求构造一个串,使得这个串是由所给的串相连接构成,连接能够有重叠的部分. 思路分析: 首先用所给的串建立自己主动机,每一个单词节点记录当前节点可以达到的最长后缀. 開始的时候想的是dp[i ...
随机推荐
- select 修改选中时候的默认默认样式 outline:none 把系统的线关了 然后自己再border一下
chrome 查看样式的时候默认没有 focus的样式,可以把选择器开开select 修改选中时候的默认默认样式 outline:none 把系统的线关了 然后自己再border一下input:foc ...
- mysql创建中文编码库
[2013/5/27 16:06:08] LuoXingchen: Create MySQL Database with Chinese supported: mysql> cre ...
- zabbix4.2学习笔记--新建用户组和用户
新建用户组 zabbix中管理机器是以用户组划分,这里新建一个只读用户群组和只读用户 新建用户组 点击 管理-用户组-创建用户群组,如下图 点击创建之后,有三列设置,分别是用户群组.权限和标签过滤器, ...
- python selenium定位总结(转)
转自:http://www.cnblogs.com/yufeihlf/p/5717291.html 父子定位元素 查找有父亲元素的标签名为span,它的所有标签名叫input的子元素 find_ele ...
- 笔试算法题(17):奇偶数分置数组前后段 & 反序访问链表
出题:输入一个数组,要求通过交换操作将奇数索引的元素调整到数组前半部分,偶数索引的元素调整到数组后半部分: 分析: 当然如果没有额外要求的话很容易实现,最好使用In-Place的实现策略:考虑插入排序 ...
- Quartz--01
Quartz 调度器(scheduler):定时定频率的去执行任务 任务(job):业务逻辑 触发器(trigger):让任务生效的时间 JobDetail(包含任务实现类,任务信息) trigger ...
- LeetCode(46)Permutations
题目 Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the fo ...
- 关于SQL Server 的限制
经常被人问到关于SQL Server 的连接数限制, 或者最大的文件大小, 或者标准版和企业版的区别,以及Express上的 其实这些问题都可以在MSDN 上直接找到 SQL Server 2014 ...
- servlet页面没有跳转
Boolean b = userService.selectByParams(user);if (b) { req.getSession().setAttribute("loginname& ...
- Web框架django进阶篇
分页 一.Django内置分页 from django.shortcuts import render from django.core.paginator import Paginator, Emp ...