您的位置:首页 > 其它

dp训练 1005

2013-09-11 14:07 316 查看

Human Gene Functions

Time Limit : 2000/1000ms(Java/Other)   MemoryLimit : 65536/32768K (Java/Other)Total Submission(s) :20   AcceptedSubmission(s) : 14

Font: Times NewRoman | Verdana | Georgia

Font Size: ← →

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 fourletters, A, C, G, and T. Biologists have been interested inidentifying human genes and determining their functions, becausethese can be used to diagnose human diseases and to design newdrugs for them. A human gene can be identified through a series of time-consumingbiological experiments, often with the help of computer programs.Once a sequence of a gene is obtained, the next job is to determineits function. One of the methods for biologists to use indetermining the function of a new gene sequence that they have justidentified is to search a database with the new gene as a query.The database to be searched stores many gene sequences and theirfunctions – many researchers have been submitting their genes andfunctions to the database and the database is freely accessiblethrough the Internet. A database search will return a list of gene sequences from thedatabase that are similar to the query gene. Biologists assume thatsequence similarity often implies functional similarity. So, thefunction of the new gene might be one of the functions that thegenes from the list have. To exactly determine which one is theright one another series of biological experiments will beneeded. Your job is to make a program that compares two genes anddetermines their similarity as explained below. Your program may beused as a part of the database search if you can provide anefficient one. Given two genes AGTGATG and GTTAG, how similar are they? One of themethods to measure the similarity of two genes is called alignment.In an alignment, spaces are inserted, if necessary, in appropriatepositions of the genes to make them equally long and score theresulting genes according to a scoringmatrix. For example, one space is inserted into AGTGATG to result inAGTGAT-G, and three spaces are inserted into GTTAG to result in–GT--TAG. A space is denoted by a minus sign (-). The two genes arenow of equal length. These two strings arealigned: AGTGAT-G -GT--TAG In this alignment, there are four matches, namely, G in the secondposition, T in the third, T in the sixth, and G in the eighth. Eachpair of aligned characters is assigned a score according to thefollowing scoring matrix.* denotes that a space-space match is not allowed. The score of thealignment 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 differentpositions): 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, thisone is optimal since no other alignment can have a higher score.So, it is said that the similarity of the two genes is14.

Input

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

Output

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

Sample Input

7 AGTGATG 
5 GTTAG 
7 AGCTATT 
9 AGCTTTAAA

Sample Output

14 
21
本题类似于最长公共子序列(LCS)问题,使用gene[][]数组记录动态规划过程中产生的中间结果。
 采用char map[128],只使用5个map['A']=0,map['C']=1,map['G']=2,map['T']=3,map['-']=4.
gene[i][j]表示基因子串str1和str2的分值;根据题意有下列关系:
(1)str1取i-1个字母str2取‘-’:
 m1=gene[i-1][j]+score[map[str1[i-1]]][4];
(2)str2取j-1个字母str1取‘-’:
gene[i][0]=gene[i-1][0]+score[map[str1[i-1]]][4];
当dp结束后gene[first][second]即为所求值。
m2=gene[i][j-1]+score[4][map[str2[j-1]]];
(3)str1取i-1个字母str2取‘j-1'字母:
gene[0][i]=gene[0][i-1]+score[4][map[str2[i-1]]];
当j=0时,即gene[0-first,0],有gene[0][0]可计算:
m3=gene[i-1][j-1]+score[map[str1[i-1]]][map[str2[j-1]]];
从m1,m2,m3取最大的存在gene[i][j]中,要考虑边界的因素,即i或j为零的时候,分三种情况:
(1)当i和j都为零的时候,gene[i][j]=0;
(2)当i=0时,即gene[0,0-second],有gene[0][0]可计算:
#include<stdio.h>
#define MAX 101
char str1[MAX],str2[MAX];
char map[128];
int gene[MAX][MAX];
int main()
{
    map['A']=0;
    map['C']=1;
    map['G']=2;
    map['T']=3;
    map['-']=4;
    int score[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,0}
    };
    int Case;
    int i,j,k;
    int first,second;
    scanf("%d",&Case);
    for(k=0;k<Case;k++)
    {
        scanf("%d%s",&first,str1);
        scanf("%d%s",&second,str2);
        gene[0][0]=0;
        for(i=1;i<=second;i++)
            gene[0][i]=gene[0][i-1]+score[4][map[str2[i-1]]];
        for(i=1;i<=first;i++)
            gene[i][0]=gene[i-1][0]+score[map[str1[i-1]]][4];
        int m1,m2,m3;
        for(i=1;i<=first;i++)
            for(j=1;j<=second;j++)
            {
                m1=gene[i-1][j]+score[map[str1[i-1]]][4];
                m2=gene[i][j-1]+score[4][map[str2[j-1]]];
                m3=gene[i-1][j-1]+score[map[str1[i-1]]][map[str2[j-1]]];
                int max=m1;
                if(m2>max) max=m2;
                if(m3>max) max=m3;
                gene[i][j]=max;
            }
            printf("%d\n",gene[first][second]);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: