当前位置: 代码迷 >> 综合 >> poj 1080Human Gene Functions(dp lcs 变形)
  详细解决方案

poj 1080Human Gene Functions(dp lcs 变形)

热度:76   发布时间:2024-01-14 21:50:06.0
Human Gene Functions
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18886   Accepted: 10518

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 su bmitting t (服从)heir genes and functions to the database and the database is freely ac cessible t (易接近的)hrough 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 de noted b (表示)y 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

14
21 

LCS的变形而已

注意LCS的子串可以是离散的,不必连续,用动态规划

 

设dp[i][j]为取s1第i个字符,s2第j个字符时的最大分值

则决定dp为最优的情况有三种(score[][]为s1[i]和s2[j]两符号的分数):

1、  s1取第i个字母,s2取“ - ”: dp[i-1][j]+score[ s1[i-1] ]['-'];

2、  s1取“ - ”,s2取第j个字母:dp[i][j-1]+score['-'][ s2[j-1] ];

3、  s1取第i个字母,s2取第j个字母:dp[i-1][j-1]+score[ s1[i-1] ][ s2[j-1] ];

 即dp[i][j]=max( dp[i-1][j]+score[ s1[i-1] ]['-'],

dp[i][j-1]+score['-'][ s2[j-1] ],

dp[i-1][j-1]+score[ s1[i-1] ][ s2[j-1] ] );

 

注意初始化

不仅仅只有

dp[0][0] = 0

也不仅仅是

dp[0][0] = 0

dp[1][0] = score[ s1[i-1] ]['-']

dp[0][1] = score['-'][ s2[j-1] ]

必须全面考虑到所有情况,

当i=j=0时,dp[i][j]=0

当i=0时,dp[0,j] = dp[0][j-1] + score['-'][ s2[j-1] ]

当j=0时,dp[i,0] = dp[i-1][0] + score[ s1[i-1] ]['-']

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3fint dp[101][101];
int socre[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,inf},};int change(char x)
{int id;switch(x){case 'A':id=0;break;case 'C':id=1;break;case 'G':id=2;break;case 'T':id=3;break;default: id=4;break;}return id;
}int max(int a,int b,int c)
{int k;k=a>b?a:b;return c>k?c:k;
}
int main()
{int t;int len1,len2;char s1[110],s2[110];int i,j,k;scanf("%d",&t);while(t--){scanf("%d %s",&len1,s1);scanf("%d %s",&len2,s2);dp[0][0]=0;for(i=1;i<=len1;i++)dp[i][0] = dp[i-1][0] + socre[ change(s1[i-1]) ][ change('-') ];for(j=1;j<=len2;j++)dp[0][j]=dp[0][j-1]+socre[change('-')][change(s2[j-1])];for(i=1;i<=len1;i++)for(j=1;j<=len2;j++){int tem1=dp[i-1][j-1]+socre[change(s1[i-1])][change(s2[j-1])];int tem2=dp[i-1][j]+socre[change(s1[i-1])][change('-')];int tem3=dp[i][j-1]+socre[change('-')][change(s2[j-1])];dp[i][j]=max(tem1,tem2,tem3);}printf("%d\n",dp[len1][len2]);}return 0;
}


  相关解决方案