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

Description

It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T. Biologists have been interested in identifying human genes and determining their functions, because
these can be used to diagnose human diseases and to design new drugs for them. 



A human gene can be identified through a series of time-consuming biological experiments, often with the help of computer programs. Once a sequence of a gene is obtained, the next job is to determine its function. 

One of the methods for biologists to use in determining the function of a new gene sequence that they have just identified is to search a database with the new gene as a query. The database to be searched stores many gene sequences and their functions – many
researchers have been submitting their genes and functions to the database and the database is freely accessible through the Internet. 



A database search will return a list of gene sequences from the database that are similar to the query gene. 

Biologists assume that sequence similarity often implies functional similarity. So, the function of the new gene might be one of the functions that the genes from the list have. To exactly determine which one is the right one another series of biological experiments
will be needed. 



Your job is to make a program that compares two genes and determines their similarity as explained below. Your program may be used as a part of the database search if you can provide an efficient one. 

Given two genes AGTGATG and GTTAG, how similar are they? One of the methods to measure the similarity 

of two genes is called alignment. In an alignment, spaces are inserted, if necessary, in appropriate positions of 

the genes to make them equally long and score the resulting genes according to a scoring matrix. 



For example, one space is inserted into AGTGATG to result in AGTGAT-G, and three spaces are inserted into GTTAG to result in –GT--TAG. A space is denoted by a minus sign (-). The two genes are now of equal 

length. These two strings are aligned: 



AGTGAT-G 

-GT--TAG 



In this alignment, there are four matches, namely, G in the second position, T in the third, T in the sixth, and G in the eighth. Each pair of aligned characters is assigned a score according to the following scoring matrix. 




denotes that a space-space match is not allowed. The score of the alignment above is (-3)+5+5+(-2)+(-3)+5+(-3)+5=9. 



Of course, many other alignments are possible. One is shown below (a different number of spaces are inserted into different positions): 



AGTGATG 

-GTTA-G 



This alignment gives a score of (-3)+5+5+(-2)+5+(-1) +5=14. So, this one is better than the previous one. As a matter of fact, this one is optimal since no other alignment can have a higher score. So, it is said that the 

similarity of the two genes is 14.

Input

The input consists of T test cases. The number of test cases ) (T is given in the first line of the input file. Each test case consists of two lines: each line contains an integer, the length of a gene, followed by a gene sequence. The length of each gene sequence
is at least one and does not exceed 100.

Output

The output should print the similarity of each test case, one per line.

Sample Input

  1. 2
  2. 7 AGTGATG
  3. 5 GTTAG
  4. 7 AGCTATT
  5. 9 AGCTTTAAA

Sample Output

  1. 14
  2. 21

题意是要找一对基因序列的最大相似度。这个最大相似度来源于每一对字母的相似度相加(方框里的),但让人蛋疼的是,每一对字母之间是可以错位的,不一定是一对一那么匹配,可以空出来一位让‘-’去和原字母匹配去,所以这样就增加了难度,要用DP。

这个题我真的是想递推关系想不出来啊啊啊,结果看了discuss中的思路才明白过来,是这样:

假设上面序列的是A1A2A3A4......Am

下面序列式B1B2B3B4....Bn,然后用f[m][n]表示A长度为m,B长度为n时的最大相似程度。

好,我们假设现在比对的是Am和Bn这里,比对这里的话,就有三个情况:

1)Am=Bn

这样结果会是:

XXXXXAm

XXXXXBn

那么此时递推的关系也就是

f[m][n]=f[m-1][n-1]+pipei(Am,Bn)

2)Am不等于Bn

这样结果可能会是

XXXXXAm

XXXXX  -

那么此时的递推关系就是

f[m][n]=f[m-1][n]+pipei(Am,'-')

3)Am不等于Bn

XXXXX  -

XXXXX Bn

此时的递推关系是f[m][n-1]+pipei('-',Bn),从这三个情况中选择最大值。

递推关系是这样的话,是必须初始化的(因为存在着最佳匹配是第一个字符与‘-’匹配的情况)

即f[m][0]=f[m-1][0]+pipei(Am,'-')

f[0][n]=f[0][n-1]+pipei('-',Bn)

代码:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <vector>
  5. #include <string>
  6. #include <cstring>
  7. #include <map>
  8. #pragma warning(disable:4996)
  9. using namespace std;
  10.  
  11. int matrix[5][5]={{5,-1,-2,-1,-3},
  12. {-1,5,-3,-2,-4},
  13. {-2,-3,5,-2,-2},
  14. {-1,-2,-2,5,-1},
  15. {-3,-4,-2,-1,0}
  16. };
  17. map<char,int>pair1;
  18. int num1,num2;
  19. char test1[150],test2[150];
  20. int dp[150][150];
  21.  
  22. void cal()
  23. {
  24. memset(dp,0,sizeof(dp));
  25.  
  26. int i,j;
  27. for(i=0;i<num1;i++)
  28. {
  29. dp[i+1][0]=dp[i][0]+matrix[pair1[test1[i]]][pair1['-']];
  30. }
  31. for(j=0;j<num2;j++)
  32. {
  33. dp[0][j+1]=dp[0][j]+matrix[pair1['-']][pair1[test2[j]]];
  34. }
  35. for(i=0;i<num1;i++)
  36. {
  37. for(j=0;j<num2;j++)
  38. {
  39. dp[i+1][j+1]= max(dp[i][j]+matrix[pair1[test1[i]]][pair1[test2[j]]],
  40. max(dp[i][j+1]+matrix[pair1[test1[i]]][pair1['-']],dp[i+1][j]+matrix[pair1['-']][pair1[test2[j]]]));
  41. }
  42. }
  43. cout<<dp[num1][num2]<<endl;
  44. }
  45.  
  46. int main()
  47. {
  48. pair1['A']=0;
  49. pair1['C']=1;
  50. pair1['G']=2;
  51. pair1['T']=3;
  52. pair1['-']=4;
  53.  
  54. int Test;
  55. scanf("%d",&Test);
  56.  
  57. while(Test--)
  58. {
  59. cin>>num1>>test1;
  60. cin>>num2>>test2;
  61.  
  62. cal();
  63. }
  64. return 0;
  65. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1080:Human Gene Functions LCS经典DP的更多相关文章

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

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

  2. poj 1080 ——Human Gene Functions——————【最长公共子序列变型题】

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

  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. dp poj 1080 Human Gene Functions

    题目链接: http://poj.org/problem?id=1080 题目大意: 给两个由A.C.T.G四个字符组成的字符串,可以在两串中加入-,使得两串长度相等. 每两个字符匹配时都有个值,求怎 ...

  5. poj 1080 Human Gene Functions(dp)

    题目:http://poj.org/problem?id=1080 题意:比较两个基因序列,测定它们的相似度,将两个基因排成直线,如果需要的话插入空格,使基因的长度相等,然后根据那个表格计算出相似度. ...

  6. POJ 1080 Human Gene Functions 【dp】

    题目大意:每次给出两个碱基序列(包含ATGC的两个字符串),其中每一个碱基与另一串中碱基如果配对或者与空串对应会有一个分数(可能为负),找出一种方式使得两个序列配对的分数最大 思路:字符串动态规划的经 ...

  7. POJ 1080 Human Gene Functions

    题意:给两个DNA序列,在这两个DNA序列中插入若干个'-',使两段序列长度相等,对应位置的两个符号的得分规则给出,求最高得分. 解法:dp.dp[i][j]表示第一个字符串s1的前i个字符和第二个字 ...

  8. poj 1080 Human Gene Functions (最长公共子序列变形)

    题意:有两个代表基因序列的字符串s1和s2,在两个基因序列中通过添加"-"来使得两个序列等长:其中每对基因匹配时会形成题中图片所示匹配值,求所能得到的总的最大匹配值. 题解:这题运 ...

  9. hdu 1080 Human Gene Functions(DP)

    题意: 人类基因由A.C.G.T组成. 有一张5*5的基因表.每格有一个值,叫相似度.例:A-C:-3.意思是如果A和C配对, 则它俩的相似度是-3[P.S.:-和-没有相似度,即-和-不能配对] 现 ...

随机推荐

  1. SpringBoo#Mybatis多个数据源配置,Sqlite&Mysql

    第一步:排除数据源的自动配置类: @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 第二步:定义好两个数据源的 ...

  2. windows下 RN 环境搭建

    01.安装 Android Studio 02.NodeJs 03.Python204.JDK 05.安装Genymotion模拟器06.java 环境配置07.andriud sdk 配置08.An ...

  3. 和为S的连续正整数序列(双指针法)

    题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...

  4. php phar反序列化任意执行代码

    2018年 原理一.关于流包装stream wrapper大多数的文件操作允许使用各种URL协议去访问文件路径,如data://,zlib://,php://例如常见的有include('php:// ...

  5. 剑指offer系列(六)

    题目描述: 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList,例如按照链表顺序,1->2->3->4->5->6->7->8,那么我们将得到{8 ...

  6. 配置vSphere Web Client超时值

    1.默认超时值120分钟 2.webclient.properties文件位置:     Windows系统:C:\ProgramData\VMware\vCenterServer\cfg\vsphe ...

  7. dede开启会员功能

    登陆后台,找到菜单里面的系统基本参数设置>会员设置>开启会员功能,选择“是”,保存即可

  8. crmv2项目

    maven -----------------------------------------------------------------------------感谢打赏!

  9. web安全(xss攻击和csrf攻击)

    1.CSRF攻击: CSRF(Cross-site request forgery):跨站请求伪造. (1).攻击原理: 如上图,在B网站引诱用户访问A网站(用户之前登录过A网站,浏览器 cookie ...

  10. 长篇Essay写作凑字数的小技巧

    当一个留学党面对一篇5000字的essay,写一半之后却没法继续~这类的感觉是很多同学无法想象的!此时唯一的一个有效的方法:凑字数!但是essay写作怎么凑字数呢?如何写够5000字essay?下面我 ...