Pku acm 1080-Human Gene Function

Source: Internet
Author: User
Source: http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1080 problem-solving report: I recently gave a fuzzy look at the dynamic planning chapter in the introduction to algorithms. I seem to have understood it. When I did the problem, I found that I still wouldn't do it, and I could not find any subproblems, I don't know how to set up the optimal sub-structure. I wanted to find a simple sub-structure and I found 1080. I still had no idea about the result. I understood a little bit after reading other people's code, it seems that more training is required. This question is to calculate the similarity between two strings. Set the first string S1 [0], S1 [1], S1 [2],... s1 [len1-1], second string S2 [0], S2 [1],... s2 [len2-1], then look at the relationship between the last two strings, there are three possibilities: 1. s1 [len1-1] corresponds to S2 [len2-1] 2. s1 [len1-1] corresponds to '-' 3. s2 [len2-1] And '-' correspond to the first case, the string similarity is S1 [0], S1 [1], S1 [2],... s1 [len1-2] and S2 [0], S2 [1],... s2 [len2-2] similarity + S1 [len1-1] and S2 [len2-1] similarity. Obviously, to reach the maximum value, S1 [0], S1 [1], S1 [2],... s1 [len1-2] and S2 [0], S2 [1],... s2 [len2-2] the similarity between the two strings also reaches the maximum value. In the second case, the similarity between the two strings is S1 [0], S1 [1], S1 [2],... s1 [len1-2] and S2 [0], S2 [1],... s2 [len2-1] similarity + S1 [len1-1] And '-' similarity. Obviously, to reach the maximum value, S1 [0], S1 [1], S1 [2],... s1 [len1-2] and S2 [0], S2 [1],... s2 [len2-1] the similarity between the two strings also reaches the maximum value. In the third case, the similarity between the two strings is S1 [0], S1 [1], S1 [2],... s1 [len1-1] and S2 [0], S2 [1],... the similarity of S2 [len2-2] + the similarity between '-' and S2 [len2-1. Obviously, to reach the maximum value, S1 [0], S1 [1], S1 [2],... s1 [len1-1] and S2 [0], S2 [1],... s2 [len2-2] the similarity between the two strings also reaches the maximum value. Use Dynamic Planning from bottom up, set f [I] [J] to indicate the similarity between the string consisting of the first I characters of string S1 and the string consisting of the first J characters of string S2, so f [I] [J] = max (F [I-1] [J-1] + value (S1 [I-1], S2 [J-1]), // first case F [I-1] [J] + value (S1 [I-1], '-'), // case 2 F [I] [J-1] + value ('-', S2 [J-1]) // case 3) # Include <iostream> <br/> using namespace STD; </P> <p> # define INF 100 </P> <p> int f [102] [102]; </P> <p> int V [5] [5] = <br/> {<br/> 5,-1,-2,-1,-3, <br/>-1, 5,-3,-2,-4, <br/>-2,-3, 5,-2,-2, <br/>-1, -2,-2, 5,-1, <br/>-3,-4,-2,-1,-INF <br/> }; </P> <p> int toint (char c) <br/>{< br/> int I; <br/> switch (c) <br/> {<br/> case 'A': I = 0; break; <br/> case 'C': I = 1; break; <br/> case 'G': I = 2; break; <br/> case 'T': I = 3; break; <br/> Case '-': I = 4; break; <br/> default: break; <br/>}< br/> return I; <br/>}</P> <p> int value (char a, char B) <br/>{< br/> int M = toint (); <br/> int n = toint (B); <br/> return V [m] [N]; <br/>}</P> <p> int max (int A, int B, int c) <br/>{< br/> int M = A <B? B: A; <br/> M = m <C? C: m; <br/> return m; <br/>}</P> <p> int main () <br/>{< br/> int casenum; <br/> CIN> casenum; <br/> while (casenum --) <br/>{< br/> int len1, len2; <br/> CIN> len1; <br/> char * S1 = new char [len1 + 1]; <br/> CIN> S1; <br/> CIN> len2; <br/> char * S2 = new char [len2 + 1]; <br/> CIN> S2; <br/> for (INT I = 0; I <= len1; I ++) <br/> {<br/> for (Int J = 0; j <= len2; j ++) <br/>{< br/> if (I = 0 & J = 0) <br/> F [I] [J] = 0; <br/> else if (I = 0) <br/> F [I] [J] = f [I] [J-1] + value ('-', S2 [J-1]); <br/> else if (j = 0) <br/> F [I] [J] = f [I-1] [J] + value (S1 [I-1], '-'); <br/> else <br/> {<br/> F [I] [J] = max (F [I-1] [J-1] + value (S1 [I-1], s2 [J-1]), f [I-1] [J] + value (S1 [I-1], '-'), f [I] [J-1] + value ('-', s2 [J-1]); <br/>}< br/> cout <F [len1] [len2] <Endl; <br/>}< br/>}Appendix: human gene functions
Time limit:1000 ms   Memory limit:10000 K
Total submissions:9149   Accepted:5045

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
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.
The image cannot be displayed in a corresponding table ..
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
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 shoshould print the similarity of each test case, one per line.

Sample Input

2 7 AGTGATG 5 GTTAG 7 AGCTATT 9 AGCTTTAAA 

Sample output

1421 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.