Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 16487 | Accepted: 9151 |
Description
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
Output
Sample Input
2 7 AGTGATG 5 GTTAG 7 AGCTATT 9 AGCTTTAAA
Sample Output
14 21
题目大意:
对于两个给定的,由‘A’,'G',‘C','T'组成的序列,可以找到这两个序列之间的一个由对应字母确定的对应关系,不同的字母之间的对应可得到不同的权值(如题中表格)。例如:对于序列AGTGATG和GTTAG,一种可能的对应关系是
A | G | T | G | A | T | - | G |
- | G | T | - | - | T | A | G |
而另一种方案:AGTGATG 与-GTTA-G可得到的权值和为(-3)+5+5+(-2)+5+(-1) +5=14.
可以看出不同的方案可以得到不同的权值和,现要求所有可能的方案可以得到的最大权值和。
解题思路:本题可以看作“最长公共子序列”问题的一个变形,使用同样的填表递推方法求解,用dp[i][j]表示序列s1的前i个字母与s2的前j个字母所能达到的最大权值和。不过每次决策不再是判断是否匹配,而是分为三种情况:
1、让s1[i]与s2[j]匹配
2、让s1[i]与'-'匹配
3、让s2[j]与'-'匹配
选其中最大的为dp[i][j]。
以map[x][y]来表示字母x对应y时的权值,状态转移方程即为dp[i][j]=max{ dp[i-1][j-1]+map[ s1[i] ][ s2[j] ] , dp[i-1][j]+map[ s1[i] ][ '-' ] , dp[i][j-1]+map['-' ][ s2[j] ] }
按此公式填表即可。
注意:初始化表格的第0行和第0列时,是以序列与空序列匹配为准,即所有字符都与'-'匹配。
代码:
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
int map[8][8]={
{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 c2n(char c)
{
if(c=='A') return 0;
if(c=='C') return 1;
if(c=='G') return 2;
if(c=='T') return 3;
return 4;
}
int dp[105][105];
int main()
{
int num;
cin>>num;
while(num--)
{
int n1,n2;
string s1,s2;
cin>>n1>>s1;
cin>>n2>>s2;
s1="0"+s1;
s2="0"+s2;
dp[0][0]=0;
for(int i=1;i<=n1;++i)
{
dp[i][0]=dp[i-1][0]+map[c2n(s1[i])][4];
}
for(int i=1;i<=n2;++i)
{
dp[0][i]=dp[0][i-1]+map[4][c2n(s2[i])];
}
for(int i=1;i<=n1;++i)
{
for(int j=1;j<=n2;++j)
{
int tem1=dp[i-1][j-1]+map[c2n(s1[i])][c2n(s2[j])];
int tem2=dp[i-1][j]+map[c2n(s1[i])][4];
int tem3=dp[i][j-1]+map[4][c2n(s2[j])];
tem1=tem1>tem2?tem1:tem2;
tem1=tem1>tem3?tem1:tem3;
dp[i][j]=tem1;
}
}
cout<<dp[n1][n2]<<endl;
}
return 0;
}