Dynamic programming to solve the--LCS variation of Hangzhou electric OJ1080 problem

Source: Internet
Author: User

First on the topic:

Human Gene FunctionsTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 2570 Accepted Submission (s): 1451


Problem DescriptionIt is well known the A human gene can be considered as a sequence, consisting of four nucleotides, whi CH is simply denoted by four letters, A, C, G, and T. Biologists has been interested in identifying human genes and dete Rmining their functions, 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 both genes is called alignment. In an alignment, spaces is inserted, if necessary, in appropriate positions of the genes-make them equally long and SC Ore 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 the similarity of the the genes is 14.

Inputthe 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 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.

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

Sample Input

Sample Output
the meaning of the title is:

Given two strings A, b, where two of the letters 22 correspond to a weight, and two strings can be arbitrarily added spaces, so that more characters can correspond, such as given two genes AGTGATG and Gttag, can be written
Agtgat-g
-gt--tag

The final corresponding weights are the score of the alignment above is (-3) +5+5+ (-2) + (-3) +5+ (-3) +5=9.
can also be written
Agtgatg
-gtta-g

The sum of the corresponding weights is (-3) +5+5+ (-2) +5+ (-1) +5=14, because the addition of spaces, can have a lot of different weights and requirements are the maximum weight and.

We see the first thought of the problem is LCS, the longest common subsequence, below the longest common sub-sequence DP diagram for the reader to compare:


And for the current topic is the variant of LCS, is a weighted value of the longest common sub-sequence, the weight requirement is to meet the following matrix and the weights and maximum:


The comparison chart with the above LCS result status is as follows:


The transfer equation is: Dp[i][j]=max (a,b,c);//Three values in the maximum, a,b,c respectively by the upper left, the left, the upper three direction of the value of a number obtained;

A=DP[I-1][J-1]+M[S1[I]][S2[J]]; M[x][y] refers to x and y corresponding value, if x=a,y=g, then m[x][y]=-2, if x=t,y= '-', then m[x][y]=-1;

b=dp[i][j-1]+m['-'][s2[j]];

c=dp[i-1][j]+m[s1[i]['-'];

Here is a reference to C + + code:

#include <iostream>//#include <string>//#include <algorithm>//#include <cmath>//#include <vector>//#include <stack>//#include <iomanip>using namespace std; #define MAX 105/* TEST data */int dp[ 105][105];int m[5][5]={//column order is acgt-{5,-1,-2,-1,-3},{-1,5,-3,-2,-4},{-2,-3,5,-2,-2},{-1,-2,-2,5,-1},{-3,-4,-2,- 1,-100}};int Mymap (char x) {if (x== ' A ') return 0;if (x== ' C ') return 1;if (x== ' G ') return 2;if (x== ' T ') return 3;return 4;} int getmax (int a,int b,int c) {int mm=a;if (B&GT;MM) mm=b;if (c>mm) mm=c;return mm;}   int main () {int t,n1,n2;   int j,i;   Char s1[105],s2[105];   cin>>t;  while (t--) {cin>>n1>>s1+1;  cin>>n2>>s2+1;  dp[0][0]=0;  s1[0]=s2[0]= '-';  for (i=1;i<=n1;i++) Dp[0][i]=dp[0][i-1]+m[4][mymap (S1[i]);  for (i=1;i<=n2;i++) Dp[i][0]=dp[i-1][0]+m[mymap (S2[i])][4]; for (i=1;i<=n2;i++) for (j=1;j<=n1;j++) Dp[i][j]=getmax ((Dp[i-1][j-1]+m[mymap (S2[i])][mymap (S1[j])]), Dp[i-1] [J]+m[mymap (S2[i])][4],dp[i][j-1]+m[4][mymap (S1[j]);cout<<dp[n2][n1]<<endl; }return 0;}


Dynamic programming to solve the--LCS variation of Hangzhou electric OJ1080 problem

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.