Hdu 1080 Human Gene Functions(最长公共子序列的变形)

来源:互联网 发布:淘宝关联页是什么 编辑:程序博客网 时间:2024/05/02 02:07

Human Gene Functions

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2552    Accepted Submission(s): 1445


Problem 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. 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
2 7 AGTGATG 5 GTTAG 7 AGCTATT 9 AGCTTTAAA
 

Sample Output
14 21
 

Source
Asia 2001, Taejon (South Korea) 




题目大意:给定两个字符串,字符串之间任意位置可以插入‘-’使得两个字符串长度相等,对于每两个字符有如下相对应的关系,,例如所给的例子:序列AGTGATG 和 GTTAG,有可能对应的关系是
AGTGAT-G
-GT--TAG     
字符‘A’和‘-’的权值是-3,‘G’和‘G’的权值是5,字符‘G’和‘-’的权值是-2等等,所以这两个字符串的权值之和是(-3)+5+5+(-2)+(-3)+5+(-3)+5=9;
另外一种情况:
AGTGATG
-GTTA-G 
这种情况下的权值之和是(-3)+5+5+(-2)+5+(-1) +5=14,这是两个字符串的权值之和最大值,所以输出结果是14。

看到这你可能想到了动态规划,我们用dp[i][j]表示字符串str1的前i个字符和str2的前j个字符所能达到的最大权值,而dp[i][j]可以有三种情况:
第一种:str1[i] 与 str2[j]相匹配,dp[i-1][j-1]+value[num(str1[i])][num(srt2[j])]
第二种:str1[i] 与 ‘-’相匹配,dp[i][j-1]+value[num(str1[i])][num('-')]
第三种:‘-’与str2[j]相匹配,dp[i-1][j]+value[num('-')][num(str2[j])]
选出其中的最大值保存到dp[i][j]。

注意:初始化时,dp[0][0]=0,然后第0行和第0列都与‘-’相匹配。


#include <iostream>#include <cstring>#include <cstdio>using namespace std;#define MAX 105int dp[MAX][MAX];int value[5][5]= {5, -1, -2, -1, -3,                -1, 5, -3, -2, -4,                -2, -3, 5, -2, -2,                -1, -2, -2, 5, -1,                -3, -4, -2, -1};int num(char ch){    switch(ch)    {        case 'A': return 0;        case 'C': return 1;        case 'G': return 2;        case 'T': return 3;        case '-': return 4;    }}int maxn(int a,int b,int c){    int temp;    temp = a>b?a:b;    if(temp > c)        return temp;    else        return c;}int main(){    int t;    int i, j;    int len1, len2;    char str1[MAX], str2[MAX];    cin>>t;    while(t--)    {        cin>>len1;        getchar();        for(i=1; i<=len1; i++)            cin>>str1[i];        cin>>len2;        getchar();        for(i=1; i<=len2; i++)            cin>>str2[i];        dp[0][0] = 0;        for(i=1; i<=len1; i++)      //初始化,每一行的第一个都与‘-’匹配            dp[i][0] = dp[i-1][0]+value[num(str1[i])][num('-')];        for(i=1; i<=len2; i++)      //每一列的第一个都与‘-’匹配            dp[0][i] = dp[0][i-1]+value[num('-')][num(str2[i])];        for(i=1; i<=len1; i++)        {            for(j=1; j<=len2; j++)            {                int temp1, temp2, temp3;                temp1 = dp[i-1][j-1]+value[num(str1[i])][num(str2[j])]; //str1[i]与str2[j]匹配                temp2 = dp[i-1][j]+value[num(str1[i])][num('-')];   //str1[i]与'-'匹配                temp3 = dp[i][j-1]+value[num('-')][num(str2[j])];   //str2[j]与'-'匹配                dp[i][j] = maxn(temp1,temp2,temp3);            }        }        cout<<dp[len1][len2]<<endl;    }    return 0;}





0 0
原创粉丝点击