Test instructions: The two ends of the gene sequence matching, four different nucleotides TCGA match when there are different scores, such as T-G score is-2, can also add spaces, spaces and nucleotides matching also have corresponding scores, to find the maximum score
Analysis:
Difficulty in the number and position of spaces is uncertain
Two-dimensional dp,dp[i][j] represents the maximum fraction of the first I segment of sequence A and the first J segment of sequence B. Next, carefully analyze the case where I and J match: 1.a[i] and B[j], 2.a[i] and b[j-1];3.a[i] and b[j+1].
So the equation:dp [i][j]= max (dp[i-1][j]+mat[i][' # '],dp[i][j-1]+mat[' # '][j],dp[i-1][j-1]+mat[i][j])
Code:
#include <iostream> #include <string> #include <cstring> #define INF 0x3f3f3f3fusing namespace Std;int T,n,m,dp[200][200];string a,b;int mat[200][200];int max (int a,int b) {return a>b?a:b;} void F () {mat[' a ' [' A ']=5;mat[' C '] [' C ']=5;mat[' g ' [' g ']=5;mat[' t '] [' t ']=5;mat[' a '] [' C ']=-1;mat[' a '] [' g ']=-2;mat[ ' A ' [' t ']=-1;mat[' a ' ['] ']=-3;mat[' C ' [' A ']=-1;mat[' C '] [' G ']=-3;mat[' C '] [' t ']=-2;mat[' C '] [' # ']=-4;mat[' G '] [' a ' ]=-2;mat[' g ' [' C ']=-3;mat[' g ' [' T ']=-2;mat[' g '] [' # ']=-2;mat[' t ' [' A ']=-1;mat[' t '] [' C ']=-2;mat[' t '] [' g ']=-2;mat [' T '] [' # ']=-1;mat[' # ' [' A ']=-3;mat[' # '] ' C ']=-4;mat[' # ' [' G ']=-2;mat[' # ' [' T ']=-1;mat[' # '] [' # ']=-inf;} int main () {f (); Cin>>t;while (t--) {cin>>n>>a;cin>>m>>b;dp[0][0]=0;for (int i=1;i<= n;i++) dp[i][0]=dp[i-1][0]+mat[a[i-1]][' # '];for (int i=1;i<=m;i++) dp[0][i]=dp[0][i-1]+mat[' # '][b[i-1]];for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {Dp[i][j]=max (Dp[i-1][j-1]+mat[a[i-1]][b[j-1]],max (dp[i-1][j]+mat[a[ i-1]][' # '],dp[i][j-1]+mat['#'] [B[j-1]]);}} Cout<<dp[n][m]<<endl;}}
HDU Human Gene functions--dp--(really drunk)