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 ...
随机推荐
- SharePoint 内容部署-PowerShell
1. 创建一个新的内容部署路径 New-SPContentDeploymentPath –Name "Marketing Internet Content" –SourceSPWe ...
- servlet其工作原理和例子证明
servlet简单介绍 当我们在地址栏里面输入www.baidu.com,终于呈如今我们面前的是百度搜索的页面.在这些訪问过程中,都会有一个webserver来处理这些请求以及訪问处理后的结果. 而s ...
- Objective-c 算术函数和常量代表
不变 常量名 说明 M_PI 圆周率(=π) M_PI_2 圆周率的1/2(=π/2) M_PI_4 圆周率的1/4(=π/4) M_1_PI =1/π M_2_PI =2/π M_E =e M_LO ...
- 【ThinkingInC++】52、函数内部的静态变量
/** * 书本:[ThinkingInC++] * 功能:函数内部的静态变量 * 时间:2014年9月17日18:06:33 * 作者:cutter_point */ #include " ...
- VSTO之旅系列(三):自定义Excel UI
原文:VSTO之旅系列(三):自定义Excel UI 本专题概要 引言 自定义任务窗体(Task Pane) 自定义选项卡,即Ribbon 自定义上下文菜单 小结 引言 在上一个专题中为大家介绍如何创 ...
- BT渗透工具使用学习笔记
BT51.信息收集2.扫描工具3.漏洞发现4.社会工程学工具5.运用层攻击MSF6.局域网攻击7.密码破解8.维持访问一.DNS信息收集1.Dnsenum/pentest/enumeration/dn ...
- 基于FP-Tree的关联规则FP-Growth推荐算法Java实现
基于FP-Tree的关联规则FP-Growth推荐算法Java实现 package edu.test.ch8; import java.util.ArrayList; import java.util ...
- hdu3664(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3664 分析:dp[i][j]表示i个数的排列中E值为j的个数.假设现在已有一个E值为j的i的排列,对于 ...
- 纯CSS实现各类气球泡泡对话框效果
原文 纯CSS实现各类气球泡泡对话框效果 一.关于纯CSS实现气泡对话框 首先,来张大图: 上边这张黄黄的,大大的,圆圆的,有个小尾巴,文字内容有些YY的图片,就是使用纯CSS实现的气泡对话框效果,一 ...
- java获取日期之间的差异
转载请注明出处.谢谢http://blog.csdn.net/harryweasley/article/details/42121485 当想到要计算差值.我们肯定想的是"2014.12.1 ...