基因修复

  题目大意:给定一些坏串,再给你一个字符串,要你修复这个字符串(AGTC随便换),使之不含任何坏串,求修复所需要的最小步数。

  这一题也是和之前的那个1625的思想是一样的,通过特殊的trie树找到所有的状态然后一个一个枚,具体状态转移的思想可以在1625那里看

  当然了这一题不是像1625那样求总的组合数,这一题也是DP,求的是最小值,那么我们也是一样,统计从合法状态中转移到任何一个状态最小值即可。

  状态转移方程dp[i+1][转移状态]=min(dp[i+1][转移状态],dp[i][当前状态]+s)(当转移状态对应的就是trie的节点,当节点对应的字符等于字符串当前位置的字符,则s为0,否则s为1。)

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <functional>
  4. #include <string.h>
  5. #define MAX 1010
  6.  
  7. using namespace std;
  8.  
  9. struct node
  10. {
  11. int Num, If_End;
  12. struct node *Fail, *Next[];
  13. }*root, Mem_Pool[MAX], *Queue[ * MAX];
  14.  
  15. static int sum_node, Hash_Table[], dp[MAX][MAX];
  16. static char str[MAX], DNA[] = { 'A', 'G', 'C', 'T' };
  17.  
  18. struct node *create_new_node();
  19. int find_min(const int, const int);
  20. void put_DNA_into_hash(void);
  21. void insert(struct node *);
  22. void build_ac_automation(struct node *);
  23.  
  24. int main(void)
  25. {
  26. int Disease_Segement_Sum, str_length, case_sum = ;
  27.  
  28. put_DNA_into_hash();
  29. while (~scanf("%d", &Disease_Segement_Sum))
  30. {
  31. if (Disease_Segement_Sum == )
  32. break;
  33. sum_node = ;
  34. node *root = create_new_node();
  35. getchar();
  36.  
  37. for (int i = ; i < Disease_Segement_Sum; i++)
  38. insert(root);
  39. build_ac_automation(root);
  40.  
  41. gets(str);
  42. str_length = strlen(str);
  43.  
  44. for (int i = ; i < str_length; i++)
  45. {
  46. fill(dp[i + ], dp[i + ] + sum_node, MAX + );
  47. for (int j = ; j < sum_node; j++)
  48. {
  49. if (Mem_Pool[j].If_End)
  50. continue;
  51. for (int k = ; k < ; k++)
  52. {
  53. int id = Mem_Pool[j].Next[k]->Num;
  54. if (Mem_Pool[j].Next[k]->If_End)
  55. continue;
  56. else if (Hash_Table[str[i]] == k)
  57. dp[i + ][id] = find_min(dp[i + ][id], dp[i][j]);
  58. else
  59. dp[i + ][id] = find_min(dp[i + ][id], dp[i][j] + );
  60. }
  61. }
  62. }
  63. int ans = MAX + ;
  64. for (int i = ; i < sum_node; i++)
  65. ans = find_min(ans, dp[str_length][i]);
  66. if (ans != MAX + )
  67. printf("Case %d: %d\n", case_sum++, ans);
  68. else
  69. printf("Case %d: -1\n",case_sum++);
  70. }
  71. return EXIT_SUCCESS;
  72. }
  73.  
  74. int find_min(const int x, const int y)
  75. {
  76. return x < y ? x : y;
  77. }
  78.  
  79. struct node *create_new_node(void)
  80. {
  81. node *tmp = &Mem_Pool[sum_node];
  82. tmp->Fail = NULL;
  83. tmp->If_End = ;
  84. memset(tmp->Next, , sizeof(struct node *) * );
  85. tmp->Num = sum_node++;
  86. return tmp;
  87. }
  88.  
  89. void put_DNA_into_hash(void)
  90. {
  91. for (int i = ; i<; i++)
  92. Hash_Table[DNA[i]] = i;
  93. }
  94.  
  95. void insert(struct node *root)
  96. {
  97. node *ptr = root;
  98. gets(str);
  99.  
  100. for (int i = ; str[i] != '\0'; i++)
  101. {
  102. int id = Hash_Table[str[i]];
  103. if (ptr->Next[id] == NULL)
  104. ptr->Next[id] = create_new_node();
  105. ptr = ptr->Next[id];
  106. }
  107. ptr->If_End = ;
  108. }
  109.  
  110. void build_ac_automation(struct node *root)
  111. {
  112. int head = , tail = ;
  113. root->Fail = NULL;
  114. Queue[tail++] = root;
  115.  
  116. while (head != tail)
  117. {
  118. node *out = Queue[head++];
  119. for (int i = ; i < ; i++)
  120. {
  121. if (out->Next[i] != NULL)
  122. {
  123. if (out == root)
  124. out->Next[i]->Fail = root;
  125. else
  126. {
  127. out->Next[i]->Fail = out->Fail->Next[i];
  128. if (out->Fail->Next[i]->If_End)
  129. out->Next[i]->If_End = ;
  130. }
  131. Queue[tail++] = out->Next[i];
  132. }
  133. else if (out == root)
  134. out->Next[i] = root;
  135. else
  136. out->Next[i] = out->Fail->Next[i];
  137. }
  138. }
  139. }

  

Match:DNA repair(POJ 3691)的更多相关文章

  1. POJ 3691 &amp; HDU 2457 DNA repair (AC自己主动机,DP)

    http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...

  2. POJ 3691 DNA repair (DP+AC自动机)

    DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4815   Accepted: 2237 Descri ...

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

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

  4. HDU 2457/POJ 3691 DNA repair AC自动机+DP

    DNA repair Problem Description   Biologists finally invent techniques of repairing DNA that contains ...

  5. HDU 2457 DNA repair(AC自动机+DP)题解

    题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...

  6. hdu2457:DNA repair

    AC自动机+dp.问改变多少个字符能让目标串不含病毒串.即走过多少步不经过病毒串终点.又是同样的问题. #include<cstdio> #include<cstring> # ...

  7. HDU 2425 DNA repair (AC自动机+DP)

    DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. 【POJ3691】 DNA repair (AC自动机+DP)

    DNA repair Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description B ...

  9. HDU2457 DNA repair —— AC自动机 + DP

    题目链接:https://vjudge.net/problem/HDU-2457 DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory ...

随机推荐

  1. 导航菜单:jQuery粘性滚动导航栏效果

    粘性滚动是当导航在滚动过程中会占粘于浏览器上,达到方便网站页面浏览的效果,也是一种用户体验,下面我们看一下是怎么实现的: jQuery的 smint插件,也是一个导航菜单固定插件.当页滚动时,导航菜单 ...

  2. Windows Server 2008 R2 IIS7.5下PHP、MySQL快速环境配置【图】

    众所周知,win平台的服务器版本默认是不能运行php的,需要对服务器进行环境配置. 而许多朋友纠结如何配置,在百度上搜索出的教程一大堆,基本步骤复杂,新手配置容易出错. 今天,邹颖峥教大家一种快速配置 ...

  3. hibernate中get,load,list,iterate的用法及比较

    首先,get和load都是查询单个对象,而list和iterate为批量查询 注意以下写法仅针对hibernate3的语法. 使用案例如下: // 1. get和load 的用法 Person p = ...

  4. 全键盘Vimium快捷键学习记录

    0.设置而 vimium 的默认搜索引擎: http://www.baidu.com/s?wd= j: 向下细微滚动窗口.  k:向上细微滚动窗口. gg:跳转到页面的顶部.G:跳转到页面的底部.r: ...

  5. 原生态js获取节点的方法

    <input value="我是用id来获取值的" type="button" onclick="GetById()"/> &l ...

  6. 跟着百度学PHP[4]OOP面对对象编程-6-封装性private

    所谓封装顾名思义,如同箱子般给封装起来.结合前面的来说就是对属性或者方法,封装后的方法或属性只能有类内部进行调用.外部调用不了. 封装性的好处: 1.信息隐藏 2.http://www.cnblogs ...

  7. TP中的四种url访问方式

    什么是PATHINFO:就是http://localhost/index.php/Home/Index/index/a/1/b/2?c=3中的红色部分, 注意c=3并不是pathinfo的一部分,它是 ...

  8. am335x 电容屏驱动添加。

    参考:http://www.cnblogs.com/helloworldtoyou/p/5530422.html 上面可以下载驱动. 解压后驱动有如下目录: 我们要选择的是: eGTouchARM/e ...

  9. BZOJ 2448: 挖油

    Description [0,x]中全是1,其余全是0,每个点有一个权值,求最坏情况下得到x的最小权值. Sol DP+单调队列. 首先就是一个 \(O(n^3)\) 的DP. \(f[i][j]\) ...

  10. BZOJ 2084: [Poi2010]Antisymmetry

    Sol Manacher. \(O(n)\) Manacher很简单啊.改一改转移就可以了. 然后我WA了.一开始天真的认为id只会是奇数,然后就GG. 一组 Hack 数据 3 1 0 0 然后就跳 ...