题意读了半年,唉,给你两串字符,然后长度不同,你能够用‘-’把它们补成同样长度,补在哪里取决于得分,它会给你一个得分表,问你最大得分

跟LCS非常像的DP数组 dp[i][j]表示第一个字符串取第i个元素第二个字符串取第三个元素,然后再预处理一个得分表加上就可以

得分表:

  1. score['A']['A'] = score['C']['C'] = score['G']['G'] = score['T']['T'] = 5;
  2.  
  3. score['A']['C'] = score['C']['A'] = -1;
  4. score['A']['G'] = score['G']['A'] = -2;
  5. score['A']['T'] = score['T']['A'] = -1;
  6. score['A']['-'] = score['-']['A'] = -3;
  7. score['C']['G'] = score['G']['C'] = -3;
  8. score['C']['T'] = score['T']['C'] = -2;
  9. score['C']['-'] = score['-']['C'] = -4;
  10. score['G']['T'] = score['T']['G'] = -2;
  11. score['G']['-'] = score['-']['G'] = -2;
  12. score['T']['-'] = score['-']['T'] = -1;
  13.  
  14. score['-']['-'] = -inf;

那么DP方程就好推了:

dp[i][j] = :

dp[i-1][j] + score[s1[i-1]]['-']或者

dp[i][j-1] + score['-'][s2[j-1]]或者

dp[i-1][j-1] + score[s1[i-1]][s2[j-1]]或者

这三者之中取最大的

然后就是边界问题我给忘记了

不够细心,若单单是i==0或者j==0,边界问题就出现了,边界不可能为0的,所以还得处理一下边界

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<list>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<string>
  7. #include<queue>
  8. #include<stack>
  9. #include<map>
  10. #include<vector>
  11. #include<cmath>
  12. #include<memory.h>
  13. #include<set>
  14. #include<cctype>
  15.  
  16. #define ll long long
  17.  
  18. #define LL __int64
  19.  
  20. #define eps 1e-8
  21.  
  22. #define inf 0xfffffff
  23.  
  24. //const LL INF = 1LL<<61;
  25.  
  26. using namespace std;
  27.  
  28. //vector<pair<int,int> > G;
  29. //typedef pair<int,int > P;
  30. //vector<pair<int,int> > ::iterator iter;
  31. //
  32. //map<ll,int >mp;
  33. //map<ll,int >::iterator p;
  34.  
  35. const int N = 1000 + 5;
  36.  
  37. int dp[N][N];
  38. int score[200][200];
  39.  
  40. void init() {
  41. score['A']['A'] = score['C']['C'] = score['G']['G'] = score['T']['T'] = 5;
  42.  
  43. score['A']['C'] = score['C']['A'] = -1;
  44. score['A']['G'] = score['G']['A'] = -2;
  45. score['A']['T'] = score['T']['A'] = -1;
  46. score['A']['-'] = score['-']['A'] = -3;
  47. score['C']['G'] = score['G']['C'] = -3;
  48. score['C']['T'] = score['T']['C'] = -2;
  49. score['C']['-'] = score['-']['C'] = -4;
  50. score['G']['T'] = score['T']['G'] = -2;
  51. score['G']['-'] = score['-']['G'] = -2;
  52. score['T']['-'] = score['-']['T'] = -1;
  53.  
  54. score['-']['-'] = -inf;
  55. }
  56.  
  57. int main () {
  58. init();
  59. int t;
  60. char s1[N];
  61. char s2[N];
  62. scanf("%d",&t);
  63. while(t--) {
  64. int n,m;
  65. memset(dp,0,sizeof(dp));
  66. scanf("%d %s",&n,s1);
  67. scanf("%d %s",&m,s2);
  68. for(int i=1;i<=n;i++)
  69. dp[i][0] = dp[i-1][0] + score[s1[i-1]]['-'];//边界处理
  70. for(int j=1;j<=m;j++)
  71. dp[0][j] = dp[0][j-1] + score['-'][s2[j-1]];//边界处理
  72. for(int i=1;i<=n;i++) {
  73. for(int j=1;j<=m;j++) {
  74. int t1 = dp[i-1][j] + score[s1[i-1]]['-'];
  75. int t2 = dp[i][j-1] + score['-'][s2[j-1]];
  76. int t3 = dp[i-1][j-1] + score[s1[i-1]][s2[j-1]];
  77. int maxn = max(t1,t2);
  78. dp[i][j] = max(maxn,t3);
  79. }
  80. }
  81. printf("%d\n",dp[n][m]);
  82. }
  83. return 0;
  84. }

POJ1080 Human Gene Functions 动态规划 LCS的变形的更多相关文章

  1. poj 1080 Human Gene Functions(lcs,较难)

    Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19573   Accepted:  ...

  2. poj1080 - Human Gene Functions (dp)

    题面 It is well known that a human gene can be considered as a sequence, consisting of four nucleotide ...

  3. POJ 1080 Human Gene Functions -- 动态规划(最长公共子序列)

    题目地址:http://poj.org/problem?id=1080 Description It is well known that a human gene can be considered ...

  4. POJ1080 Human Gene Functions(LCS)

    题目链接. 分析: 和 LCS 差不多. #include <iostream> #include <cstdio> #include <cstdlib> #inc ...

  5. POJ-1080 Human Gene Functions---类似LCS

    题目链接: https://cn.vjudge.net/problem/POJ-1080 题目大意: 给定两组序列,要你求出它们的最大相似度,每个字母与其他字母或自身和空格对应都有一个打分,求在这两个 ...

  6. poj1080--Human Gene Functions(dp:LCS变形)

    Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17206   Accepted:  ...

  7. POJ 1080:Human Gene Functions LCS经典DP

    Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18007   Accepted:  ...

  8. hdu1080 Human Gene Functions() 2016-05-24 14:43 65人阅读 评论(0) 收藏

    Human Gene Functions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  9. Human Gene Functions

    Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18053 Accepted: 1004 ...

随机推荐

  1. 元素z-index继承问题

    两同层元素.当中一个的子元素定位与还有一个重叠时,该子元素被覆盖(此时子元素的z-index继承的是其父元素的z-index,不管其z-index多少均被覆盖) <html> <he ...

  2. Swift正在使用NSURLConnection异步下载同步(实例解析)

    原版的blog.转载请注明出处 http://blog.csdn.net/hello_hwc 一.同步异步两个概念 简单来讲.同步就是函数或者闭包(objective c中的block)运行完成才干返 ...

  3. Linux shell用法和技巧(转)

    使用Linux shell是我每天的基本工作,但我经常会忘记一些有用的shell命令和l技巧.当然,命令我能记住,但我不敢说能记得如何用它执行某个特定任务.于是,我开始在一个文本文件里记录这些用法,并 ...

  4. nodeValue的兼容问题

    nodeValue获取Text或Comment元素的文本值. 在IE6.IE7.IE8中游览器会自作聪明的去掉前面的空白字符text,而其它现代游览器则会保留空白 <body> <s ...

  5. Javascript设计模式系列二

    创建对象的基本模式,一.门户大开型,二.采用下划线来表示属性和方法的私用性,三.使用闭包来创建私用的成员. 一.门户大开型.只能提供公用成员.所有属性和方法都公开的.可访问的.这些共用属性都要使用th ...

  6. AutoFac使用方法总结:Part I

    注册部分 使用RegisterType进行注册 [Fact] public void can_resolve_myclass() { var builder = new ContainerBuilde ...

  7. 算法——字符串匹配Rabin-Karp算法

    前言 Rabin-Karp字符串匹配算法和前面介绍的<朴素字符串匹配算法>类似,也是相应每一个字符进行比較.不同的是Rabin-Karp採用了把字符进行预处理,也就是对每一个字符进行相应进 ...

  8. 返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作

    原文:返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, ...

  9. Android截图

    Android截图很好的实现,从文档的发展,查看View有一个接口getDrawingCache(),这个接口可以得到View当调用这个接口的位图图像Bitmap. 抓取截图View在图像的某一个时刻 ...

  10. Oracle得知(十五):分布式数据库

    --分布式数据库的独立性:分布数据的独立性指用户不必关心数据怎样切割和存储,仅仅需关心他须要什么数据. --本地操作 SQL> sqlplus scott/tiger --远程操作 SQL> ...