Human Gene Functions
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2733 Accepted Submission(s): 1545
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 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.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/5d14276bf1efe76383ea05ede5021b14.gif)
* 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. 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
Source
题目大意:
给定各种DNA元素两两匹配度的值,可以加空格,但是空格不能和空格匹配,求最大匹配总和最大是多少。
解题思路:
类比最长公共子序列很容易写出如下动态转移方程,dp[i][j]代表第一个串的第i个位置和第二串的第j个位置匹配度的和.
状态转移方程:
dp[i][j] = dp[i-1][j] + dist[s1[i],'-']; //第一个子序列和第二个空格匹配之后,所增加的值为dist[s1[i],'-']
dp[i][j] = dp[i][j-1] + dist['-',s2[j]]; //第二个子序列和第一个子序列空格匹配之后,所增加的值为dist['-',s2[j]];
dp[i][j] = dp[i-1][j-1] + dist[s1[i],s2[j]]; //第一个子序列和第二个子序列元素匹配之后,所增加的值为dist[s1[i],s2[j]];
边界:
很容易知道dp[0][0] = 0,因为没东西匹配.
dp[i][0],第二个串只能都是空格,所以累加
dp[0][j],第一个串只能都是空格,所以也是累加.
AC代码:
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 105;
const int inf = 0x3f3f3f3f;
char str1[maxn],str2[maxn];
int dist[maxn][maxn];
int dp[maxn][maxn];
int s1[maxn],s2[maxn];
int a,c,g,t,f; //分别代表A,C,G,T,-
int m,n;
void build() //建立键值关系
{
a = 0;
c = 1;
g = 2;
t = 3;
f = 4; //把f当成'-'
dist[a][a] = 5;dist[a][c] = -1;dist[a][g] = -2;dist[a][t] = -1;dist[a][f] = -3;
dist[c][a] = -1;dist[c][c] = 5;dist[c][g] = -3;dist[c][t] = -2;dist[c][f] = -4;
dist[g][a] = -2;dist[g][c] = -3;dist[g][g] = 5;dist[g][t] = -2;dist[g][f] = -2;
dist[t][a] = -1;dist[t][c] = -2;dist[t][g] = -2;dist[t][t] = 5;dist[t][f] = -1;
dist[f][a] = -3;dist[f][c] = -4;dist[f][g] = -2;dist[f][t] = -1;
}
void tran()
{
int i;
for(i=1;i<=m;i++)
{
if(str1[i] == 'A')
{
s1[i] = 0;
}
else if(str1[i] == 'C')
{
s1[i] = 1;
}
else if(str1[i] == 'G')
{
s1[i] = 2;
}
else if(str1[i] == 'T')
{
s1[i] = 3;
}
else if(str1[i] == '-')
{
s1[i] = 4;
}
}
for(i=1;i<=n;i++)
{
if(str2[i] == 'A')
{
s2[i] = 0;
}
else if(str2[i] == 'C')
{
s2[i] = 1;
}
else if(str2[i] == 'G')
{
s2[i] = 2;
}
else if(str2[i] == 'T')
{
s2[i] = 3;
}
else if(str2[i] == '-')
{
s2[i] = 4;
}
}
}
int main()
{
int T;
int i,j;
build();
//freopen("1.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d",&m);
scanf("%s",str1+1);
scanf("%d",&n);
scanf("%s",str2+1);
tran();
dp[0][0] = 0;
for(i=1;i<=m;i++)
{
dp[i][0] = dp[i-1][0] + dist[s1[i]][f];
}
for(i=1;i<=n;i++)
{
dp[0][i] = dp[0][i-1] + dist[f][s2[i]];
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
dp[i][j] = -inf;
dp[i][j] = max(dp[i][j],dp[i-1][j] + dist[s1[i]][f]);
dp[i][j] = max(dp[i][j],dp[i][j-1] + dist[f][s2[j]]);
dp[i][j] = max(dp[i][j],dp[i-1][j-1] + dist[s1[i]][s2[j]]);
}
}
printf("%d\n",dp[m][n]);
}
return 0;
}