Problem Description
It is well known that a human gene can be considered as a sequence, consisting of four nucleus otides, 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 into gene sequences and their functions-your 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, incluother 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 shoshould 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
Input
2
7. AGTGATG
5. GTTAG
7. AGCTATT
9 AGCTTTAAA
Output
14
21
Question: two strings, each of which can be inserted with '-' to ensure that the last two strings are of the same length and then alignment the two strings, the two strings of letters at the same position match a value, and the maximum and sum of these values is the deformation of the first question.
Table of values obtained by character matching
Dp [I] [j] represents the maximum value of pairing 0 to I-1 with 0 to J-1
Status transfer:
① Dp [I] [j] is transferred from dp [I-1] [j] at the cost of a [I-1] matching '-'
② Transferred by dp [I] [J-1], the price is B [J-1] with '-' match
3 by dp [I-1] [J-1] transferred, the price is a [I-1] with B [J-1] matching
[Html]
# Include <stdio. h>
# Include <string. h>
Int max [500] [500];
Int map [150] [150];
Void match ()
{
Map ['a'] ['a'] = 5; map ['a'] ['C'] =-1; map ['a'] ['G'] =-2; map ['a'] ['T'] =-1; map ['a'] ['-'] =-3;
Map ['C'] ['a'] =-1; map ['C'] ['C'] = 5; map ['C'] ['G'] =-3; map ['C'] ['T'] =-2; map ['C'] ['-'] =-4;
Map ['G'] ['a'] =-2; map ['G'] ['C'] =-3; map ['G'] ['G'] = 5; map ['G'] ['T'] =-2; map ['G'] ['-'] =-2;
Map ['T'] ['a'] =-1; map ['T'] ['C'] =-2; map ['T'] ['G'] =-2; map ['T'] ['T'] = 5; map ['T'] ['-'] =-1;
Map ['-'] ['a'] =-3; map ['-'] ['C'] =-4; map ['-'] ['G'] =-2; map ['-'] ['T'] =-1; // map ['-'] ['-'] =-3;
}
Int main ()
{
Int I, j, t, len1, len2;
Char q1 [500], q2 [500];
Match ();
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d % s", & len1, q1 + 1 );
Scanf ("% d % s", & len2, q2 + 1 );
Memset (max, 0, sizeof (max ));
Max [0] [0] = 0;
For (I = 1; I <= len1; I ++)
// Max [I] [0] = map [q1 [I] ['-']; // The initialization error here is Amitabha.
Max [I] [0] = max [I-1] [0] + map [q1 [I] ['-'];
For (j = 1; j <= len2; j ++)
// Max [0] [j] = map ['-'] [q2 [j];
Max [0] [j] = max [0] [J-1] + map ['-'] [q2 [j];
For (I = 1; I <= len1; I ++)
For (j = 1; j <= len2; j ++)
{
Max [I] [j] = (max [I-1] [J-1] + map [q1 [I] [q2 [j])> (max [I-1] [j] + map [q1 [I] ['-'])?
(Max [I-1] [J-1] + map [q1 [I] [q2 [j]) :( max [I-1] [j] + map [q1 [I] ['-']);
Max [I] [j] = max [I] [j]> (max [I] [J-1] + map ['-'] [q2 [j])? Max [I] [j] :( max [I] [J-1] + map ['-'] [q2 [j]);
} Www.2cto.com
Printf ("% d \ n", max [len1] [len2]);
}
Return 0;
}
Author: hnust_xiehonghao