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 ...
随机推荐
- Andy's First Dictionary
Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy ...
- httpclient超时总结(转)
Httpclient超时 背景: 网站这边多次因为httpclient调用超时时间没设置好导致关掉,影响非常不好,而且问题重复出现,查看网络,没有比较明确介绍httpclient所有超时相关的设置(大 ...
- hdu5179(数位dp)
传送门:beautiful number 题意:令 A=∑ni=1ai?10n?i(1≤ai≤9)(n为A的位数).若A为“漂亮的数”当且仅当对于任意1≤i<n满足a[i]≥a[i+1]且对于任 ...
- 实现ListView A~Z快速索引
ListView A~Z快速索引这种效果在通信录和城市列表中经常看到,方便用户查找,是一种增加用户体验的好方法. 实现步骤: 1.自定义一个名叫SlideBar 的View. 2.在布局文件中加入这个 ...
- UltraEdit破解方法最强收录
作为一个能够满足你一切编辑需求的强大文本编辑器.ultraedit在IT届有着非常高的人气.只是它正版的价钱也是不廉价滴,没记错的话是要好几十刀. 那么对于我们来说,破解UltraEdit就是一项必备 ...
- 浅谈Storm流式处理框架(转)
Hadoop的高吞吐,海量数据处理的能力使得人们可以方便地处理海量数据.但是,Hadoop的缺点也和它的优点同样鲜明——延迟大,响应缓慢,运维复杂. 有需求也就有创造,在Hadoop基本奠定了大数据霸 ...
- SVN Error: “' 'x' isn't in the same repository as 'y' ” during merge (并不在同一个版本库中)
在使用svn merge命令报错 英文版本:SVN Error: “' 'x' isn't in the same repository as 'y' ” during merge 中文版本报错:并不 ...
- spring web.xml配置服务启动后执行文件
<bean id="readXmlService" class="com.xxx.xxx.readXmlServiceImpl" init-method= ...
- Coreseek:indexer crashed不解之谜
前两天浩哥让我再把Coreseek的索引再做一次,由于需求那边有点变化,要把索引的公司名字显示出来,就在配置文件中面加入了sql_field_string:字符串字段.. 这个属性特别好用,由于它不仅 ...
- Hot Days Codeforces Round #132 (Div. 2) D(贪婪)
Description The official capital and the cultural capital of Berland are connected by a single road ...