标题效果:

鉴于DNA有一个正确的顺序值。请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大。它被认为是负的输出噼啪。

。。

IDEAS:

施工顺序是,ac己主动机上走,求最大要用到dp

dp[i][j][k] 表示如今构造到了长度 i 。

此时的我们把当前字符放在j节点。而且满足了k状态。k是一个10位的2进制状态压缩。

注意这道题上有坑就是一个序列可能有多个权值。

所以不能直接赋值。须要用位或。

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <utility>
  6. #define inf 0x3f3f3f3f
  7. #define debug puts("fuck")
  8. using namespace std;
  9.  
  10. const char tab = 0;
  11. const int max_next = 4;
  12. int idx;
  13. struct trie
  14. {
  15. struct trie *fail;
  16. struct trie *next[max_next];
  17. int isword;
  18. int index;
  19. };
  20. int rev[256];
  21. trie *que[100005],ac[100005];
  22. int head,tail;
  23. trie *New()
  24. {
  25. trie *temp=&ac[idx];
  26. for(int i=0;i<max_next;i++)temp->next[i]=NULL;
  27. temp->fail=NULL;
  28. temp->isword=0;
  29. temp->index=idx++;
  30. return temp;
  31. }
  32. void Insert(trie *root,char *word,int len,int ind){
  33. trie *t=root;
  34. for(int i=0;i<len;i++){
  35. if(t->next[rev[word[i]]]==NULL)
  36. t->next[rev[word[i]]]=New();
  37. t=t->next[rev[word[i]]];
  38. }
  39. t->isword|=(1<<(ind-1));
  40. }
  41.  
  42. void acbuild(trie *root){
  43. int head=0,tail=0;
  44. que[tail++]=root;
  45. root->fail=NULL;
  46. while(head<tail){
  47. trie *temp=que[head++],*p;
  48. for(int i=0;i<max_next;i++){
  49. if(temp->next[i]){
  50. if(temp==root)temp->next[i]->fail=root;
  51. else {
  52. p=temp->fail;
  53. while(p!=NULL){
  54. if(p->next[i]){
  55. temp->next[i]->fail=p->next[i];
  56. break;
  57. }
  58. p=p->fail;
  59. }
  60. if(p==NULL)temp->next[i]->fail=root;
  61. }
  62. if(temp->next[i]->fail->isword)temp->next[i]->isword|=temp->next[i]->fail->isword;
  63. que[tail++]=temp->next[i];
  64. }
  65. else if(temp==root)temp->next[i]=root;
  66. else temp->next[i]=temp->fail->next[i];
  67. }
  68. }
  69. }
  70. void del(trie *root)
  71. {
  72. for(int i=0;i<max_next;i++)
  73. if(root->next[i])del(root->next[i]);
  74. free(root);
  75. }
  76. char word[105];
  77. bool dp[1010][1035];
  78. bool tmp[1010][1035];
  79. int val[15];
  80.  
  81. void tra()
  82. {
  83. for(int i=0;i<idx;i++)
  84. {
  85. if(ac[i].fail!=NULL)printf("fail = %d ",ac[i].fail->index);
  86. for(int k=0;k<max_next;k++)
  87. printf("%d ",ac[i].next[k]->index);
  88. puts("");
  89. }
  90. }
  91. int solve(int len,int n)
  92. {
  93. int ans=-0x3f3f3f3f;
  94.  
  95. memset(dp,false,sizeof dp);
  96.  
  97. dp[0][0]=true;
  98. for(int i=1;i<=len;i++)
  99. {
  100. for(int j=0;j<idx;j++)
  101. for(int k=0;k<(1<<n);k++)
  102. tmp[j][k]=false;
  103.  
  104. for(int j=0;j<idx;j++)
  105. {
  106. for(int k=0;k<(1<<n);k++)
  107. {
  108. if(dp[j][k])
  109. {
  110. for(int p=0;p<max_next;p++)
  111. {
  112. int q=ac[j].next[p]->index;
  113. int st=k;
  114. if(ac[j].next[p]->isword)st|=ac[j].next[p]->isword;
  115. tmp[q][st]=true;
  116. }
  117. }
  118. }
  119. }
  120. for(int j=0;j<idx;j++)
  121. for(int k=0;k<(1<<n);k++)
  122. {
  123. dp[j][k]=tmp[j][k];
  124. }
  125. }
  126. for(int j=0;j<idx;j++)
  127. {
  128. for(int k=0;k<(1<<n);k++)
  129. {
  130. if(dp[j][k])
  131. {
  132. int sum=0;
  133. for(int p=0;p<n;p++)
  134. {
  135. if((1<<p)&k)sum+=val[p+1];
  136. }
  137. ans=max(ans,sum);
  138. }
  139. }
  140. }
  141. return ans;
  142. }
  143. int main()
  144. {
  145. rev['A']=0;
  146. rev['T']=1;
  147. rev['C']=2;
  148. rev['G']=3;
  149. int n,I;
  150. while(scanf("%d%d",&n,&I)!=EOF)
  151. {
  152. idx=0;
  153. trie *root = New();
  154.  
  155. for(int i=1;i<=n;i++)
  156. {
  157. int key;
  158. scanf("%s%d",word,&key);
  159. if(strlen(word)>I)continue;
  160. Insert(root,word,strlen(word),i);
  161. val[i]=key;
  162. }
  163.  
  164. acbuild(root);
  165. int ans=solve(I,n);
  166. if(ans>=0)printf("%d\n",ans);
  167. else puts("No Rabbit after 2012!");
  168. }
  169. return 0;
  170. }

版权声明:本文博主原创文章,博客,未经同意不得转载。

Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)的更多相关文章

  1. hdu4057 Rescue the Rabbit(AC自己主动机+DP)

    Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  2. 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 ...

  3. ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)

    题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...

  4. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  5. HDU - 2825 Wireless Password(AC自己主动机+DP)

    Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...

  6. Hdu 3341 Lost&#39;s revenge (ac+自己主动机dp+hash)

    标题效果: 举个很多种DNA弦,每个字符串值值至1.最后,一个长字符串.要安排你最后一次另一个字符串,使其没事子值和最大. IDEAS: 首先easy我们的想法是想搜索的!管她3721..直接一个字符 ...

  7. hdu4758 Walk Through Squares (AC自己主动机+DP)

    Walk Through Squares Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...

  8. poj 3691 DNA repair(AC自己主动机+dp)

    DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5877   Accepted: 2760 Descri ...

  9. HDU - 4758 Walk Through Squares (AC自己主动机+DP)

    Description   On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...

随机推荐

  1. SharePoint 内容部署-PowerShell

    1. 创建一个新的内容部署路径 New-SPContentDeploymentPath –Name "Marketing Internet Content" –SourceSPWe ...

  2. servlet其工作原理和例子证明

    servlet简单介绍 当我们在地址栏里面输入www.baidu.com,终于呈如今我们面前的是百度搜索的页面.在这些訪问过程中,都会有一个webserver来处理这些请求以及訪问处理后的结果. 而s ...

  3. 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 ...

  4. 【ThinkingInC++】52、函数内部的静态变量

    /** * 书本:[ThinkingInC++] * 功能:函数内部的静态变量 * 时间:2014年9月17日18:06:33 * 作者:cutter_point */ #include " ...

  5. VSTO之旅系列(三):自定义Excel UI

    原文:VSTO之旅系列(三):自定义Excel UI 本专题概要 引言 自定义任务窗体(Task Pane) 自定义选项卡,即Ribbon 自定义上下文菜单 小结 引言 在上一个专题中为大家介绍如何创 ...

  6. BT渗透工具使用学习笔记

    BT51.信息收集2.扫描工具3.漏洞发现4.社会工程学工具5.运用层攻击MSF6.局域网攻击7.密码破解8.维持访问一.DNS信息收集1.Dnsenum/pentest/enumeration/dn ...

  7. 基于FP-Tree的关联规则FP-Growth推荐算法Java实现

    基于FP-Tree的关联规则FP-Growth推荐算法Java实现 package edu.test.ch8; import java.util.ArrayList; import java.util ...

  8. hdu3664(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3664 分析:dp[i][j]表示i个数的排列中E值为j的个数.假设现在已有一个E值为j的i的排列,对于 ...

  9. 纯CSS实现各类气球泡泡对话框效果

    原文 纯CSS实现各类气球泡泡对话框效果 一.关于纯CSS实现气泡对话框 首先,来张大图: 上边这张黄黄的,大大的,圆圆的,有个小尾巴,文字内容有些YY的图片,就是使用纯CSS实现的气泡对话框效果,一 ...

  10. java获取日期之间的差异

    转载请注明出处.谢谢http://blog.csdn.net/harryweasley/article/details/42121485 当想到要计算差值.我们肯定想的是"2014.12.1 ...