POJ-1080 Human Gene Functions 解题报告

POJ - 1080

题目描述

Human Gene Functions

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
这里写图片描述
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

2
7 AGTGATG
5 GTTAG
7 AGCTATT
9 AGCTTTAAA

Sample Output

14
21

题目大意

计算基因相似度。观察表格我们发现当两个字母相同时,获得5个相似度,若不相同,则相似度为负。在题目中,应用到这个’-‘,但实际在解题中,我们并不需要实际地操作这个字符串来达到添加’-‘的目的,假设它存在,再将相应的字符配对,来获得一个对应的相似度,取其中的最大相似度作为输出。值得注意的有这么几点:

  1. 每个字符串的长度不超过100
  2. 每个字符串配对时只会出现三种情况,一,1字符串与2字符串的相应字母配对,二,1字符串与’-‘配对,三,2字符串与’-‘配对。

解题方法

这是一道DP题目,状态为1字符串的前 i (0 <= i <= m)个字符与2字符串的前 j (0 <= j <= n)个字符可以得到的最大相似度,开一个二维数组score[105][105]表示相应的相似度。另外,用一个矩阵数组表示那个配对矩阵,方便起见,可以开到足够大,用相应的字母的ASCII码表示数组下标,从而直接通过访问字符串元素的方式来完成配对所需要的工作。接下来看DP过程:

  • 边界条件:score[0][0] = 0,两个空字符串的相似度为0,接下来递推的完成score[i][0]和score[0][j],即空字符串与另一个非空字符串的相似度,空字符串全部配对为’-‘。
  • 转移方程:这是本题的难点。在题目大意中已经提到,配对时只会出现三种情况,因此,我们选择三种情况中最大的那个,于是可以利用求三者中的最大值来获得递归方程。

最后,输出score[m][n],即为两字符串的相似度。

代码如下

#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 105;
int T;
int m, n;
char gene1[MAX], gene2[MAX];
int score[MAX][MAX] = {0};
int matrix[MAX][MAX];
int main()
{
    matrix['A']['A'] =  5; matrix['A']['C'] = -1; matrix['A']['G'] = -2; matrix['A']['T'] = -1; matrix['A']['-'] = -3;
    matrix['C']['A'] = -1; matrix['C']['C'] =  5; matrix['C']['G'] = -3; matrix['C']['T'] = -2; matrix['C']['-'] = -4;
    matrix['G']['A'] = -2; matrix['G']['C'] = -3; matrix['G']['G'] =  5; matrix['G']['T'] = -2; matrix['G']['-'] = -2;
    matrix['T']['A'] = -1; matrix['T']['C'] = -2; matrix['T']['G'] = -2; matrix['T']['T'] =  5; matrix['T']['-'] = -1;
    matrix['-']['A'] = -3; matrix['-']['C'] = -4; matrix['-']['G'] = -2; matrix['-']['T'] = -1; matrix['-']['-'] =  0;
    cin >> T;
    for(int t = 0; t < T; t ++)
    {
        cin >> m >> gene1;
        cin >> n >> gene2;
        score[0][0] = 0;
        memset(score, 0, sizeof(score));
        for(int i = 1; i <= m; i ++)
            score[i][0] = score[i - 1][0] + matrix[gene1[i - 1]]['-'];
        for(int j = 1; j <= n; j ++)
            score[0][j] = score[0][j - 1] + matrix['-'][gene2[j - 1]];
        for(int i = 1; i <= m; i ++)
            for(int j = 1; j <= n; j ++)
            {
                score[i][j] = score[i - 1][j - 1] + matrix[gene1[i - 1]][gene2[j - 1]];
                score[i][j] = max(score[i][j], score[i - 1][j] + matrix[gene1[i - 1]]['-']);
                score[i][j] = max(score[i][j], score[i][j - 1] + matrix['-'][gene2[j - 1]]);
            }
        cout << score[m][n] << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41480330/article/details/81671512