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

Problem Description
Dr. X is a biologist, who likes rabbits very much and can do everything for them. 2012 is coming, and Dr. X wants to take some rabbits to Noah's Ark, or there are no rabbits any more.



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.
Input
There are multiple test cases. For each case the first line is two integers n (1 ≤ n ≤ 10),l (1 ≤ l ≤ 100), indicating the number of the particular gene segment and the length of rabbits' genes.



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.
Output
For each test case, output an integer indicating the W value of the best rabbit. If we found this value is negative, you should output "No Rabbit after 2012!".
Sample Input
  1. 2 4
  2. ATG 4
  3. TGC -3
  4. 1 6
  5. TGC 4
  6. 4 1
  7. A -1
  8. T -2
  9. G -3
  10. C -4
Sample Output
  1. 4
  2. 4
  3. No Rabbit after 2012!
  4. Hint
  5. case 1we can find a rabbit whose gene string is ATGG(4), or ATGA(4) etc.
  6. case 2we can find a rabbit whose gene string is TGCTGC(4), or TGCCCC(4) etc.
  7. case 3any gene string whose length is 1 has a negative W.
  8.  
Author
HONG, Qize
Source
Recommend
lcy | We have carefully selected several similar problems for you:4053

pid=4056">
4056
4059 4052 4051


顶层模型:AC自己主动机。状态转移

解题思路:用dp[i][j][k]表示长度为i,状态为j,n个串状态为k的最大值。
注意两点:1.题目中每一个串仅仅记一次。所以每次走fail指针,把符合要求的累加就可以。
2.dp数组大小为100*1000*1024开不下,用滚动数组避免MLE。


  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #define inf 1000000000
  6. using namespace std;
  7. int n,l,w;
  8. char s[110];
  9. int L,rt,next[1010][26],end[1010],fail[1010],c[1010];
  10. int dp[2][1010][1<<10];
  11. int newnode(){
  12. memset(next[L],0,sizeof next[L]);
  13. c[L]=0;
  14. end[L++]=0;
  15. return L-1;
  16. }
  17. void init(){
  18. L=0;
  19. rt=newnode();
  20. }
  21. void insert(char *s,int z,int x){
  22. int le=strlen(s),now=rt;
  23. for(int i=0;i<le;i++){
  24. int x=s[i]-'A';
  25. if(!next[now][x]) next[now][x]=newnode();
  26. now=next[now][x];
  27. }
  28. end[now]+=z;
  29. c[now]|=(1<<x);
  30. }
  31. void build(){
  32. queue<int> q;
  33. int x=rt;
  34. for(int i=0;i<26;i++){
  35. if(next[x][i]){
  36. fail[next[x][i]]=rt;
  37. q.push(next[x][i]);
  38. }
  39. }
  40. while(!q.empty()){
  41. x=q.front();
  42. q.pop();
  43. for(int i=0;i<26;i++){
  44. if(!next[x][i]){
  45. next[x][i]=next[fail[x]][i];
  46. }else{
  47. fail[next[x][i]]=next[fail[x]][i];
  48. q.push(next[x][i]);
  49. }
  50. }
  51. }
  52. }
  53. void read(){
  54. for(int i=0;i<n;i++){
  55. scanf("%s %d",s,&w);
  56. insert(s,w,i);
  57. }
  58. }
  59. int f[4]={0,2,6,19};
  60. void solve(){
  61. build();
  62. int cur=0;
  63. for(int j=0;j<L;j++){
  64. for(int k=0;k<(1<<n);k++) dp[cur][j][k]=-inf;
  65. }
  66. dp[cur][0][0]=0;
  67. int ans=-inf;
  68. for(int i=1;i<=l;i++){
  69. for(int j=0;j<L;j++){
  70. for(int k=0;k<(1<<n);k++) dp[1-cur][j][k]=-inf;
  71. }
  72. for(int j=0;j<L;j++){
  73. for(int k=0;k<(1<<n);k++){
  74. if(dp[cur][j][k]==-inf) continue;
  75. for(int p=0;p<4;p++){
  76. int z=f[p],nxt=next[j][z];
  77. int C=0,tmp=0,x=nxt;
  78. while(x){
  79. C|=c[x];
  80. if((k&c[x])==0) tmp+=end[x];
  81. x=fail[x];
  82. }
  83. dp[1-cur][nxt][k|C]=max(dp[1-cur][nxt][k|C],dp[cur][j][k]+tmp);
  84. }
  85. }
  86. }
  87. cur=1-cur;
  88. if(i==l){
  89. for(int j=0;j<L;j++){
  90. for(int k=0;k<(1<<n);k++){
  91. ans=max(ans,dp[cur][j][k]);
  92. }
  93. }
  94. }
  95. }
  96. if(ans<0) puts("No Rabbit after 2012!");
  97. else printf("%d\n",ans);
  98. }
  99. int main(){
  100. while(~scanf("%d%d",&n,&l)){
  101. init();
  102. read();
  103. solve();
  104. }
  105. return 0;
  106. }

hdu4057 Rescue the Rabbit(AC自己主动机+DP)的更多相关文章

  1. Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)

    标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...

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

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

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

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

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

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

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

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

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

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

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

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

  8. HDU - 4511 小明系列故事――女友的考验(AC自己主动机+DP)

    Description 最终放寒假了,小明要和女朋友一起去看电影.这天,女朋友想给小明一个考验,在小明正准备出发的时候.女朋友告诉他.她在电影院等他,小明过来的路线必须满足给定的规则:  1.如果小明 ...

  9. Codeforces 86C Genetic engineering (AC自己主动机+dp)

    题目大意: 要求构造一个串,使得这个串是由所给的串相连接构成,连接能够有重叠的部分. 思路分析: 首先用所给的串建立自己主动机,每一个单词节点记录当前节点可以达到的最长后缀. 開始的时候想的是dp[i ...

随机推荐

  1. nginx 服务器

    1.windows版本的nginx启动报错 No mapping for the Unicode character exists in the target multi-byte code page ...

  2. Flask框架 之模版

    一.过滤器 safe:禁用转义: <p>{{ '<em>hello</em>' | safe }}</p> capitalize:把变量值的首字母转成大 ...

  3. 20181017 work - Inno Setup 禅道 xp系统 楷体字体 win10倒计时 nwjs chosen

    win10 闹钟和时钟 用个倒计时 Inno Setup 编译器 把应用程序打包成安装程序 nwjs-v0.14.7-win-ia32 XP专用 chrome用的49的版本 作用是把浏览器地址的程序打 ...

  4. SVN文件库移植(转)

     SVN文件库移植(转) 分类: 项目管理2013-04-19 11:06 161人阅读 评论(0) 收藏 举报 公司以前用的SVN是安装在windows2003下,用了一年多,现在大家觉得很慢,强烈 ...

  5. 如何手写一款KOA的中间件来实现断点续传

    本文实现的断点续传只是我对断点续传的一个理解.其中有很多不完善的地方,仅仅是记录了一个我对断点续传一个实现过程.大家应该也会发现我用的都是一些H5的api,老得浏览器不会支持,以及我并未将跨域考虑入内 ...

  6. 解决 python No migrations to apply 无法生成表

    第一步: 删除该app名字下的migrations文件. 第二步: 进入数据库,找到django_migrations的表,删除该app名字的所有记录. delete from django_migr ...

  7. MapReduce实例——查询缺失扑克牌

    问题: 解决: 首先分为两个过程,Map过程将<=10的牌去掉,然后只针对于>10的牌进行分类,Reduce过程,将Map传过来的键值对进行统计,然后计算出少于3张牌的的花色 1.代码 1 ...

  8. 【memcached】memcached中flags字段的作用

    我们一般只注意到memcached的数据结构是key,value,今天看memcached源代码的时候,盯上了flags,没看明白.后来问了一下同事,说PHP当中使用flags标记,标识memcach ...

  9. 如何用nfs命令烧写内核和文件系统(网络下载文件到nandflash)(未完)

    使用tftp下载烧写 a.设uboot里的ip地址 set ipaddr 192.168.1.17(uboot的ip设置成同网段) set serverip 192.168.1.5(电脑本机作为服务i ...

  10. Java中static、final、static final的区别

    final: final可以修饰:属性,方法,类,局部变量(方法中的变量) final修饰的属性的初始化可以在编译期,也可以在运行期,初始化后不能被改变. final修饰的属性跟具体对象有关,在运行期 ...