Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)
标题效果:
鉴于DNA有一个正确的顺序值。请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大。它被认为是负的输出噼啪。
。。
IDEAS:
施工顺序是,ac己主动机上走,求最大要用到dp
dp[i][j][k] 表示如今构造到了长度 i 。
此时的我们把当前字符放在j节点。而且满足了k状态。k是一个10位的2进制状态压缩。
注意这道题上有坑就是一个序列可能有多个权值。
所以不能直接赋值。须要用位或。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <utility>
#define inf 0x3f3f3f3f
#define debug puts("fuck")
using namespace std; const char tab = 0;
const int max_next = 4;
int idx;
struct trie
{
struct trie *fail;
struct trie *next[max_next];
int isword;
int index;
};
int rev[256];
trie *que[100005],ac[100005];
int head,tail;
trie *New()
{
trie *temp=&ac[idx];
for(int i=0;i<max_next;i++)temp->next[i]=NULL;
temp->fail=NULL;
temp->isword=0;
temp->index=idx++;
return temp;
}
void Insert(trie *root,char *word,int len,int ind){
trie *t=root;
for(int i=0;i<len;i++){
if(t->next[rev[word[i]]]==NULL)
t->next[rev[word[i]]]=New();
t=t->next[rev[word[i]]];
}
t->isword|=(1<<(ind-1));
} void acbuild(trie *root){
int head=0,tail=0;
que[tail++]=root;
root->fail=NULL;
while(head<tail){
trie *temp=que[head++],*p;
for(int i=0;i<max_next;i++){
if(temp->next[i]){
if(temp==root)temp->next[i]->fail=root;
else {
p=temp->fail;
while(p!=NULL){
if(p->next[i]){
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)temp->next[i]->fail=root;
}
if(temp->next[i]->fail->isword)temp->next[i]->isword|=temp->next[i]->fail->isword;
que[tail++]=temp->next[i];
}
else if(temp==root)temp->next[i]=root;
else temp->next[i]=temp->fail->next[i];
}
}
}
void del(trie *root)
{
for(int i=0;i<max_next;i++)
if(root->next[i])del(root->next[i]);
free(root);
}
char word[105];
bool dp[1010][1035];
bool tmp[1010][1035];
int val[15]; void tra()
{
for(int i=0;i<idx;i++)
{
if(ac[i].fail!=NULL)printf("fail = %d ",ac[i].fail->index);
for(int k=0;k<max_next;k++)
printf("%d ",ac[i].next[k]->index);
puts("");
}
}
int solve(int len,int n)
{
int ans=-0x3f3f3f3f; memset(dp,false,sizeof dp); dp[0][0]=true;
for(int i=1;i<=len;i++)
{
for(int j=0;j<idx;j++)
for(int k=0;k<(1<<n);k++)
tmp[j][k]=false; for(int j=0;j<idx;j++)
{
for(int k=0;k<(1<<n);k++)
{
if(dp[j][k])
{
for(int p=0;p<max_next;p++)
{
int q=ac[j].next[p]->index;
int st=k;
if(ac[j].next[p]->isword)st|=ac[j].next[p]->isword;
tmp[q][st]=true;
}
}
}
}
for(int j=0;j<idx;j++)
for(int k=0;k<(1<<n);k++)
{
dp[j][k]=tmp[j][k];
}
}
for(int j=0;j<idx;j++)
{
for(int k=0;k<(1<<n);k++)
{
if(dp[j][k])
{
int sum=0;
for(int p=0;p<n;p++)
{
if((1<<p)&k)sum+=val[p+1];
}
ans=max(ans,sum);
}
}
}
return ans;
}
int main()
{
rev['A']=0;
rev['T']=1;
rev['C']=2;
rev['G']=3;
int n,I;
while(scanf("%d%d",&n,&I)!=EOF)
{
idx=0;
trie *root = New(); for(int i=1;i<=n;i++)
{
int key;
scanf("%s%d",word,&key);
if(strlen(word)>I)continue;
Insert(root,word,strlen(word),i);
val[i]=key;
} acbuild(root);
int ans=solve(I,n);
if(ans>=0)printf("%d\n",ans);
else puts("No Rabbit after 2012!");
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)的更多相关文章
- hdu4057 Rescue the Rabbit(AC自己主动机+DP)
		
Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
 - ZOJ - 3228 Searching the String (AC自己主动机)
		
Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...
 - ZOJ 3494  BCD Code  (AC自己主动机 + 数位DP)
		
题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...
 - 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 ...
 
随机推荐
- Function 详解(一)
			
一直想写一系列关于javascript的东西,可惜从申请博客以来就一直抽不出时间来好好写上一番,今天终于熬到周末,是该好好整理一下,那么先从声明函数开始吧; 总所周知,在javascript中有匿名函 ...
 - 为Delphi程序增加UAC功能(每个步骤都很详细)
			
相关资料:http://bbs.csdn.net/topics/320071356# 操作方法: 在Source\VCL目录下应该有这样两个文件sample.manifest和WindowsXP.rc ...
 - [Android学习笔记]页面布局
			
线性布局:LinearLayout 1.集成ViewGroup,故可容纳多个View 2.线性布局,可设置水平或者垂直方向 相对布局:RelativeLayout
 - String、StringBuffer与StringBuilder差分
			
的位置不言而喻.那么他们究竟有什么优缺点,究竟什么时候该用谁呢?以下我们从以下几点说明一下 1.三者在运行速度方面的比較:StringBuilder > StringBuffer > ...
 - 不可不知的DIP、IoC、DI以及IoC容器
			
面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.当中.OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.DI以及Ioc容器等概念. 本文首先用实例阐述四个概 ...
 - 怎样在Linux下通过ldapsearch查询活动文件夹的内容
			
从Win2000開始.微软抛弃NT域而採用活动文件夹来管理Windows域.而活动文件夹就是微软基于遵守LDAP协议的文件夹服务.假设用扫描器扫描的话能够发现活动文件夹的389port是打开的.并且微 ...
 - PHPExcel融入ZF2
			
下载PHPExcel至vendor下一个 在public\index.php加拿大 require './vendor/Classes/PHPExcel.php'; 之后就能够在不论什么地方按例如以下 ...
 - C++:抽象基类和纯虚函数的理解
			
转载地址:http://blog.csdn.net/acs713/article/details/7352440 抽象类是一种特殊的类,它是为了抽象和设计的目的为建立的,它处于继承层次结构的较上层. ...
 - (转)一篇很不错的介绍Eclipse插件Menu及其扩展点的文章
			
原文在:http://tech.ddvip.com/2010-04/1271054623150507.html 菜单是各种软件及开发平台会提供的必备功能,Eclipse 也不例外,提供了丰富的菜单,包 ...
 - AMDU恢复ASM磁盘组数据(測)
			
--umount ASMCMD> umoung -a asmdg commands: md_backup, md_restor lsattr, setattr ...