Human Gene Functions
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.
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.
Examples
Input
2 7 AGTGATG 5 GTTAG 7 AGCTATT 9 AGCTTTAAA
Output
14 21
正确解法:
题目也太长了吧qaq
题目的意思是说 有两组基因串,他们的长度不等。让你往短的里面填 ‘-’ 使两个基因串一一对应。其中对应法则是上面的那个图。
求如何添加 ‘-’ 使一一对应之后得到的整数最大。
刚开始看很难写,随机加符号呀。后来看了题解(不好意思)。
还是求最长公共子序列。
设 f[i][j]表示的是前i个的s1 前 j个s2所得到的东西。
我们首先预处理嘛,把AGCT都变成0123.‘-’是4。
f[i][j]=max( f[i-1][j-1]+store[s1[i]][s2[j]]
f[i-1][j]+store[s1[i]]['-']
f[i][j-1]+store[‘-’][s2[j]] );
肯定会有f[0][i]和f[i][0]这种东西的存在。这是什么意思呢:第一个串到 s1[i],第二个串没开始就全部是 ‘-’。
于是这样后来就又把s1和s2的范围从 0~n-1 提到了 1~n.
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<map> 6 #include<set> 7 #include<algorithm> 8 #include<cmath> 9 #include<cstdlib> 10 using namespace std; 11 int ss[5][5] = { {5,-1,-2,-1,-3}, 12 {-1,5,-3,-2,-4}, 13 {-2,-3,5,-2,-2}, 14 {-1,-2,-2,5,-1}, 15 {-3,-4,-2,-1,-9999999} }; 16 int t,n,m; 17 char s1[110], s2[110]; 18 int f[110][110]; 19 void solve() 20 { 21 for (int i = 0; i < n; i++) 22 { 23 if (s1[i] == 'A') 24 s1[i] = 0; 25 else if (s1[i] == 'C') 26 s1[i] = 1; 27 else if (s1[i] == 'G') 28 s1[i] = 2; 29 else if (s1[i] == 'T') 30 s1[i] = 3; 31 } 32 for (int i = 0; i < m; i++) 33 { 34 if (s2[i] == 'A') 35 s2[i] = 0; 36 else if (s2[i] == 'C') 37 s2[i] = 1; 38 else if (s2[i] == 'G') 39 s2[i] = 2; 40 else if (s2[i] == 'T') 41 s2[i] = 3; 42 } 43 for (int i = n; i >= 1; i--) 44 s1[i] = s1[i - 1]; 45 for (int i = m; i >= 1; i--) 46 s2[i] = s2[i - 1]; 47 } 48 int main() 49 { 50 scanf("%d",&t); 51 while (t--) 52 { 53 scanf("%d %s",&n,&s1); 54 scanf("%d %s",&m,&s2); 55 memset(f, 0, sizeof(f)); 56 solve(); 57 for (int i = 1; i <= n; i++) 58 f[i][0] = f[i - 1][0] + ss[s1[i]][4]; 59 for (int i = 1; i <= m; i++) 60 f[0][i] = f[0][i - 1] + ss[4][s2[i]]; 61 for(int i=1;i<=n;i++) 62 for (int j = 1; j <= m; j++) 63 { 64 f[i][j] = f[i - 1][j - 1] + ss[s1[i]][s2[j]]; 65 f[i][j] = max(f[i][j],f[i-1][j]+ss[s1[i]][4]); 66 f[i][j] = max(f[i][j],f[i][j-1] + ss[4][s2[j]]); 67 } 68 cout << f[n][m] << endl; 69 } 70 71 return 0; 72 }
哇我刚才试了一下,没有非得提s1,s2的必要。把store里面s1,s2的值减一下就好了。
1 f[i][j] = f[i - 1][j - 1] + ss[s1[i-1]][s2[j-1]]; 2 f[i][j] = max(f[i][j],f[i-1][j]+ss[s1[i-1]][4]); 3 f[i][j] = max(f[i][j],f[i][j-1] + ss[4][s2[j-1]]);