POJ

来源:互联网 发布:东北农业大学网络登陆 编辑:程序博客网 时间:2024/06/01 08:01
                        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 

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
2 7 AGTGATG 5 GTTAG 7 AGCTATT 9 AGCTTTAAA 
Sample Output
1421 
题意:给一张DNA序列相似度值的矩阵,给两个DNA序列,可以在两个串任意位置放"-",让你求最大相似值。
思路:通过观察我们发现,最大值里面的所有增益(正匹配值)的和就是最长公共子序列的长度×5;并且如果去掉两个串串尾的一部分剩下的子串的最大匹配值不受影响,这就符合DP的无后效性;再者,完整串的最大匹配值可以由子串最大值推知,符合最有子结构和重叠子问题,我们借鉴一下最长公共子序列的思路“dp[i][j]表示s串从1到i且t串从1到j这个子问题它的最大匹配值,那么状态如何转移呢?我们站在dp[i][j]的角度思考我的最大值由哪里得来呢?
 1:s[i]觉得它跟t[j]匹配的时候会给dp[i][j]带来最大值(t[j]也觉得是这样):dp[i][j]=dp[i-1][j-   1]+G[s[i]][t[j]];
 2:s[i]觉得它跟"-"匹配的话可以得到dp[i][j]的最大值:dp[i][j]=dp[i-1][j]+G[s[i]]["-"];
 3:t[j]觉得它跟"-"匹配可以得到dp[i][j]的最大值:dp[i][j]=dp[i][j-1]+G["-"][t[j]];
   注意:初始化dp[i][j]时可以认为s和t都在跟全空格的串匹配:dp[i][0]=dp[i-1][0]+G[s[i]]['-']
 dp[0][j]=dp[0][j-1]+G[t[j]]['-']。
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define maxn 110#define INF 0x3f3f3f3fint G['T'+2]['T'+2];int dp[maxn][maxn];char s[maxn],t[maxn];void inti(){   G['A']['A']=G['T']['T']=G['C']['C']=G['G']['G']=5;   G['A']['C']=G['C']['A']=G['A']['T']=G['T']['A']=G['T']['-']=G['-']['T']=-1;   G['A']['G']=G['G']['A']=G['C']['T']=G['T']['C']=G['G']['T']=G['T']['G']=G['G']['-']=G['-']['G']=-2;   G['C']['G']=G['G']['C']=G['A']['-']=G['-']['A']=-3;   G['C']['-']=G['-']['C']=-4;   G['-']['-']=INF;}int main(){    int T;    int ls,lt;    scanf("%d",&T);            inti();        for(int i=1;i<=T;i++){            scanf("%d",&ls);            scanf("%s",s+1);            scanf("%d",<);            scanf("%s",t+1);            memset(dp,0,sizeof(dp));            for(int i=1;i<=ls;i++) dp[i][0]=dp[i-1][0]+G[s[i]]['-'];            for(int j=1;j<=lt;j++) dp[0][j]=dp[0][j-1]+G[t[j]]['-'];            for(int i=1;i<=ls;i++){                for(int j=1;j<=lt;j++){                    dp[i][j]=max(dp[i-1][j-1]+G[s[i]][t[j]],dp[i-1][j]+G[s[i]]['-']);                    dp[i][j]=max(dp[i][j],dp[i][j-1]+G['-'][t[j]]);                }            }            cout<<dp[ls][lt]<<endl;        }}