dp[i][j] 它表示的长度 i 下游前缀 j 更改节点的最小数量。

很清楚dp[0][0] = 0;

dp[ i ][ j ] = min(dp[ i ][ j ],dp[i-1][k] + (j == k ?

0 : 1)),当且仅当j。k满足下列条件时。

j 不为某条模式串的末节点 且 j 到 root 的由失败指针组成的路径上无末节点。

j 是k的儿子节点 或者 j 的父节点可由 k 沿着失败指针找到。

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <queue>
  7. #include <cmath>
  8. #include <stack>
  9. #include <map>
  10.  
  11. #pragma comment(linker, "/STACK:1024000000");
  12. #define EPS (1e-8)
  13. #define LL long long
  14. #define ULL unsigned long long
  15. #define _LL __int64
  16. #define INF 0x3f3f3f3f
  17.  
  18. using namespace std;
  19.  
  20. const int MAXN = 4;
  21.  
  22. struct N
  23. {
  24. int next[MAXN],flag,fail;
  25. } st[1010];
  26.  
  27. int Top;
  28.  
  29. int sel(char c)
  30. {
  31. if(c == 'A')
  32. return 0;
  33. if(c == 'G')
  34. return 1;
  35. if(c == 'C')
  36. return 2;
  37. return 3;
  38. }
  39.  
  40. int creat()
  41. {
  42. memset(st[Top].next,-1,sizeof(st[Top].next));
  43. st[Top].fail = -1,st[Top].flag = 0;
  44. return Top++;
  45. }
  46.  
  47. int dp[1010][1010];
  48.  
  49. char s[1010];
  50.  
  51. void Get_Trie(int root,char *s)
  52. {
  53. int site = 1;
  54.  
  55. while(s[site] != '\0')
  56. {
  57. if(st[root].next[sel(s[site])] == -1)
  58. st[root].next[sel(s[site])] = creat();
  59. root = st[root].next[sel(s[site])];
  60. ++site;
  61. }
  62.  
  63. st[root].flag = 1;
  64. }
  65.  
  66. int Get_Fail(int site,int tar)
  67. {
  68. while(site != -1 && st[site].next[tar] == -1)
  69. site = st[site].fail;
  70. if(site == -1)
  71. return 0;
  72. return st[site].next[tar];
  73. }
  74.  
  75. queue<int> q;
  76.  
  77. void Get_Fail(int root)
  78. {
  79. q.push(root);
  80.  
  81. st[root].fail = -1;
  82.  
  83. int f;
  84.  
  85. while(q.empty() == false)
  86. {
  87. f = q.front();
  88. q.pop();
  89.  
  90. for(int i = 0; i < MAXN; ++i)
  91. {
  92. if(st[f].next[i] != -1)
  93. {
  94. st[st[f].next[i]].fail = Get_Fail(st[f].fail,i);
  95. q.push(st[f].next[i]);
  96. }
  97. }
  98. }
  99. }
  100.  
  101. bool Is_Safe(int site)
  102. {
  103. if(site == -1)
  104. return true;
  105. if(st[site].flag || Is_Safe(st[site].fail) == false)
  106. return false;
  107. return true;
  108. }
  109.  
  110. int Is_Safe(int site,int tar)
  111. {
  112.  
  113. if(site == -1)
  114. return 0;
  115. if(st[site].next[tar] != -1)
  116. {
  117. if(st[st[site].next[tar]].flag != 0 || Is_Safe(st[site].next[tar]) == false)
  118. return -1;
  119. return st[site].next[tar];
  120. }
  121. return Is_Safe(st[site].fail,tar);
  122. }
  123.  
  124. void Match(int root,char *s)
  125. {
  126. memset(dp,INF,sizeof(dp));
  127. dp[0][0] = 0;
  128.  
  129. int site,i,j,tmp;
  130.  
  131. for(site = 1; s[site] != '\0'; ++site)
  132. {
  133. for(i = 0; i < Top; ++i)
  134. {
  135. if(dp[site-1][i] == INF)
  136. continue;
  137.  
  138. for(j = 0; j < 4; ++j)
  139. {
  140. if(st[i].next[j] != -1 && st[st[i].next[j]].flag != 0)
  141. continue;
  142. if(st[i].next[j] != -1 && Is_Safe(st[i].next[j]))
  143. {
  144. dp[site][st[i].next[j]] = min(dp[site][st[i].next[j]],dp[site-1][i] + (j == sel(s[site]) ? 0 : 1));
  145. continue;
  146. }
  147.  
  148. tmp = Is_Safe(i,j);
  149.  
  150. if(tmp == -1)
  151. continue;
  152. dp[site][tmp] = min(dp[site][tmp],dp[site-1][i] + (j == sel(s[site]) ? 0 : 1));
  153. }
  154. }
  155. }
  156. }
  157.  
  158. int main()
  159. {
  160. int i,n;
  161.  
  162. int icase = 1;
  163.  
  164. int root;
  165.  
  166. while(scanf("%d",&n) && n)
  167. {
  168. Top = 0;
  169. root = creat();
  170.  
  171. for(int i = 1; i <= n; ++i)
  172. {
  173. scanf("%s",s+1);
  174. Get_Trie(root,s);
  175. }
  176.  
  177. Get_Fail(root);
  178.  
  179. scanf("%s",s+1);
  180.  
  181. Match(root,s);
  182.  
  183. int anw = INF,len = strlen(s+1);
  184.  
  185. for(i = 0; i < Top; ++i)
  186. anw = min(anw,dp[len][i]);
  187. printf("Case %d: %d\n",icase++,anw == INF ? -1 : anw);
  188. }
  189.  
  190. return 0;
  191. }

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

POJ 3691 DNA repair 基于AC自己主动机DP的更多相关文章

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

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

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

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

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

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

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

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

  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. hdu4057 Rescue the Rabbit(AC自己主动机+DP)

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

  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. POJ 3691 DNA repair (DP+AC自动机)

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

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

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

随机推荐

  1. Net分布式系统

    Net分布式系统 Net分布式系统之三:Keepalived+LVS+Nginx负载均衡之高可用 摘要: 上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡, ...

  2. A Game of Thrones(2) - Catelyn

    Catelyn had never liked this godswood(神木林). She had been born a Tully, at Riverrun far to the south, ...

  3. Effective C++:条款28:避免返回 handles 指向对象内部成员

    (一) 有时候为了让一个对象尽量小,能够把数据放在另外一个辅助的struct中,然后再让一个类去指向它.看以下的代码: class Point { public: Point(int x, int y ...

  4. android2.2应用开发之IccCard(sim卡或USIM卡)

    tyle="margin:20px 0px 0px; font-size:14px; line-height:26px; font-family:Arial; color:rgb(51,51 ...

  5. Java中的反射——(1)什么是反射

    Java程序中的各个Java类属于同一类事物,描写叙述这类事物的Java类名就是Class. public class ReflectTest { public static void main(St ...

  6. POJ 1002 487-3279 Trie解读

    这个问题的解决方法是多种多样的.如本文所用,Trie为了解决这个问题. 它也可用于hash表.map等解决方案,由于输入是特定7数字,因此,你应该能够解决. 如本文所用,Trie不是非常快.最后,我主 ...

  7. Android 反编译(一,apktool+smail2java)

    一:解压缩(获取图片等资源) 对于apk中丰富的资源,假设我们在练习的时候须要引用某些apk中的资源文件时,最简单的办法使用解压缩工具对apk进行解压缩,然后在对应的文件夹下查找须要的资源文件. 二: ...

  8. 玩转web之json(五)---将表单通过serialize()方法获取的值转成json

    form表单有一个serialize()方法,可以序列化表单的值,但是jquery提供的这个方法会把数据序列化为类似下面的形式: a=1&b=2&c=3&d=4 jquery并 ...

  9. C/C++大型项目错误管理

    在C/C++大型项目中,错误管理在项目中起着举足轻重的作用,以我自己的项目经验以及观摩其它项目,错误管理对项目框架以及开发效率有着非常大的影响.对于错误管理的认识大致分为三类: 刚刚開始敲代码的新手, ...

  10. response.setHeader各种使用方法

    一秒刷新页面一次 response.setHeader("refresh","1"); 二秒跳到其它页面 response.setHeader("re ...