Poj1080 Problem Solving report

Source: Internet
Author: User

Question: http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1080

Solution: Classic dynamic DP !!!

Observe the optimal solution given by the question:

Agtgatg-The GTTA-G cut it from a certain place, if the left part of the score is not the largest, then adjust it, so that its score increases, the entire solution score increases, conflict with known optimum. Therefore, the left part must have the largest value. Likewise, the right side is also. It can be seen that the optimal sub-structure is satisfied. Consider using DP: Set the two DNA sequences to S1 and S2, respectively, with the length of len1, len2, and score as the score table. F [I, j] indicates the scores of S1 [1. I] and S2 [1. J. Consider an F [I, j]. We have: 1. s1 take the I letter, S2 take "-": F [I-1, J] + score [S1 [I], '-'] 2. s1 take "-", S2 take J letter: F [I, J-1] + score ['-', S2 [J] 3. s1 take the I letter, S2 take the J letter: F [I-1, J-1] + score [S1 [I], S2 [J] That is f [I, j] = max (F [I-1, J] + score [S1 [I], '-'], F [I, J-1] + score ['-', s2 [J], F [I-1, J-1] + score [S1 [I], S2 [J]); then consider the boundary condition, this is a case where I or J is 0. When I = J = 0, it is f [0, 0], which is used in the calculation of F, f [] = f [] + score [S1 [I], S2 [J] indicates that f [] = 0. When I = 0, that is, F [0, 1 .. len2], with f [0, 0], can be calculated using F [0, J] = f [0, J-1] + Table ['-', S2 [J. When J = 0, F [1 .. len1, 0], with f [0, 0], you can use F [I, 0] = f [I-1, 0] + Table [S1 [I],. As for the computational order, as long as the calculation of F [I, j] is ensured, F [I-1, J], F [I, J-1], F [I-1, j-1] are calculated out on the line. The so-called division stage is to achieve this goal. In this way, we can use a dual loop.
Code:
#include <stdio.h>#include <string.h>int f[101][101];int matrix[5][5];void intial(){matrix[0][0]=5;matrix[0][1]=-1;matrix[0][2]=-2;matrix[0][3]=-1;matrix[0][4]=-3;matrix[1][0]=-1;matrix[1][1]=5;matrix[1][2]=-3;matrix[1][3]=-2;matrix[1][4]=-4;matrix[2][0]=-2;matrix[2][1]=-3;matrix[2][2]=5;matrix[2][3]=-2;matrix[2][4]=-2;matrix[3][0]=-1;matrix[3][1]=-2;matrix[3][2]=-2;matrix[3][3]=5;matrix[3][4]=-1;matrix[4][0]=-3;matrix[4][1]=-4;matrix[4][2]=-2;matrix[4][3]=-1;matrix[4][4]=0;}int compare(char s1,char s2){int x,y;if(s1=='A')x=0;else if(s1=='C')x=1;else if(s1=='G')x=2;else if(s1=='T')x=3;else if(s1=='-')x=4;if(s2=='A')y=0;else if(s2=='C')y=1;else if(s2=='G')y=2;else if(s2=='T')y=3;else if(s2=='-')y=4;return matrix[x][y];}int getMax(int i,int j,char s1[],char s2[]){int ans1 = f[i][j-1]+compare('-',s2[j-1]);int ans2 = f[i-1][j]+compare(s1[i-1],'-');int ans3 = f[i-1][j-1]+compare(s1[i-1],s2[j-1]);int max = ans1;if(max<ans2)max = ans2;if(max<ans3)max=ans3;f[i][j] = max;return max;}int main(){int i,j,t,k;scanf("%d",&t);intial();for(k=0;k<t;k++){int len1,len2;char s1[101],s2[101];scanf("%d%s",&len1,s1);scanf("%d%s",&len2,s2);f[0][0] = 0;for(j=1;j<=len2;j++)f[0][j] = f[0][j-1]+compare('-',s2[j-1]);for(i=1;i<=len1;i++) f[i][0] = f[i-1][0]+compare(s1[i-1],'-');for(i=1;i<=len1;i++)for(j=1;j<=len2;j++)getMax(i,j,s1,s2);printf("%d/n",f[len1][len2]);}return 0;}

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.