Poj1080--lcs Deformation, DP

Source: Internet
Author: User

Poj1080--lcs Variant, Dphuman Gene Functions
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 17610 Accepted: 9821

Description

It's well known a human gene can considered as a sequence, consisting of four nucleotides, which was simply Denot Ed by four letters, A, C, G, and T. Biologists has been interested in identifying human genes and determining their funct Ions, because these can be used to diagnose human diseases and to design new drugs for them.

A human gene can 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 was to determine its function.
One of the methods for biologists to use in determining the function of a new gene sequence that they has just identified IS-search a database with the new gene as a query. The database to is searched stores many gene sequences and their functions–many researchers have been submitting their g Enes and functions to the database and the database are freely accessible through the Internet.

A database search would return a list of gene sequences from the database that is similar to the query gene.
Biologists assume that sequence similarity often implies functional similarity. So, the function of the new gene might is one of the functions that the genes from the list has. To exactly determine which one are the right one another series of biological experiments would be needed.

Your job is for make a program, compares, genes and determines their similarity as explained below. Your program is used as a part of the database search if you can provide an efficient one.
Given genes AGTGATG and Gttag, how similar is they? One of the methods to measure the similarity
Of genes is called alignment. In an alignment, spaces is 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 was inserted into AGTGATG to the result in agtgat-g, and three spaces was inserted into gttag to result In–gt--tag. A space is denoted by a minus sign (-). The genes is now of equal
Length. These strings is aligned:

Agtgat-g
-gt--tag

In this alignment, there is 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 are assigned a score according to the following scoring matrix.

Denotes that a space-space match was not allowed. The score of the alignment above is (-3) +5+5+ (-2) + (-3) +5+ (-3) +5=9.

Of course, many other alignments is possible. One is shown below (a different number of spaces be 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 was optimal since no other alignment can has a higher score. So, it's said that
Similarity of the 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 Lines:each line contains a integer, the length of a gene, followed by a gene sequence. The length of each gene sequence are at least one and does not exceed 100.

Output

The output should print the similarity of each test case, one per line.

Sample Input

Sample Output

1421
Test instructions: To find the similarity between the two genes, first in each gene to add a number of spaces, and then followed by the matching degree, see above table, then the similarity is the largest matching degree and (the language is not good, this section Baidu copied. )
LCS variant, dp[i][j]=max{dp[i-1][j-1]+sco[i][j],dp[i-1][j]+sco[s1[i]]['],dp[i][j-1]+sco['][sco[s2[i]}
The above brackets in the three formulas, the first easy to understand, do not explain, the second, if let S2[j] and S1[i-1] match, then only in S2[j] after the space, let S1[i] and space matching, and dp[i-1][j] is already calculated results, so do not tube s2[j-1] With whom, this is the second formula, the third one.
Another thing to note is that initialization, dp[0][0]=0,dp[0][i] This is the result of filling the space in front of the s1[0] when the match is s1[0] and s2[i], because it is the first time to calculate instead of using the results already calculated, so it must be calculated, note is not 0,
Can directly utilize the state transfer equation dp[0][i]=dp[0][i-1]+sco["][sco[s2[i]"
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<stdlib.h>using namespacestd;Const intmaxn=1010;Const intInf= (1<< -);intT;intn1,n2;intS1[MAXN],S2[MAXN];intsco[5][5]={    5,-1,-2,-1,-3,    -1,5,-3,-2,-4,    -2,-3,5,-2,-2,    -1,-2,-2,5,-1,    -3,-4,-2,-1,-INF};intDP[MAXN][MAXN];intT[MAXN];intMAX3 (intAintBintc) {    if(b>a) a=b; if(c>a) a=C; returnA;}intMain () {CIN>>T; t['A']=0; t['C']=1; t['G']=2; t['T']=3; t[' ']=4;  while(t--){        Charch; CIN>>N1;  for(intI=1; i<=n1;i++) {cin>>ch; S1[i]=T[ch]; } CIN>>N2;  for(intI=1; i<=n2;i++) {cin>>ch; S2[i]=T[ch]; } dp[0][0]=0;  for(intI=1; i<=n1;i++) {//Note that initialization, 0 before the fill of the space to get Dp[0][i]dp[i][0]=dp[i-1][0]+sco[s1[i]][4]; }         for(intI=1; i<=n2;i++) {dp[0][i]=dp[0][i-1]+sco[s2[i]][4]; }         for(intI=1; i<=n1;i++){             for(intj=1; j<=n2;j++) {Dp[i][j]=MAX3 (dp[i-1][j-1]+sco[s1[i]][s2[j]],dp[i-1][j]+sco[s1[i]][4],dp[i][j-1]+sco[4][s2[j]]); }} cout<<dp[n1][n2]<<Endl; }    return 0;}
View Code



Poj1080--lcs Deformation, DP

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.