Test instructions: The maximum similarity can be obtained by changing two strings to the same length.
Idea: This can be said to be a transformation of the editing distance, the editing distance from the final state to two strings exactly the same, this is required length, and this only allows the insertion of the "-" character. Simulates the editing distance definition State, Dp[i][j] represents the maximum similarity that the first character of a string is to the same length as the first J character of the second string. The similarity of two letters is G[I][J];
That state shifts to dp[i][j] = max (Dp[i][j-1] + g[j][5], D[i-1][j] + g[i][5],dp[i-1][j-1] + g[i][j]) The first two delegates insert "-", the latter represents a direct match.
Note: This question I wa two times, in defining the initial state dp[i][0] and dp[0][i], I forgot the cumulative consumption, but also over the sample ... Later, I gave an example to correct it.
The code is as follows:
#include <iostream>#include<cstdio>#include<cstring>#include<map>using namespacestd;#defineN 220Map<Char,int>m;intG[n][n];voidInit ();///The similarity of the initialization is obtainedCharA[n],b[n];intSolve (intLenaintLenB) { intDp[n][n]; CharTMPA,TMPB; intnuma,numb,sum; Memset (DP,0,sizeof(DP)); Sum=0; for(inti =1; I <= lenb;i++) {TMPB=B[i]; Numb=M[TMPB]; Sum+ = g[5][NUMB];///don't forget that the consumption is cumulativedp[0][i] =sum; } Sum=0; for(inti =1; I <= lena;i++) {Tmpa=A[i]; Numa=M[tmpa]; Sum+ = g[numa][5]; dp[i][0] =sum; } for(inti =1; I <= lena;i++){ for(intj =1; J <= lenb;j++) {Tmpa= A[I],TMPB =B[j]; Numa= M[tmpa],numb =M[TMPB]; DP[I][J]= Max (dp[i-1][j]+g[numa][5],dp[i][j-1]+g[5][numb]); DP[I][J]= Max (dp[i][j],dp[i-1][j-1]+G[numa][numb]); } } returnDp[lena][lenb];}voidInput () {intt,n,mm; scanf ("%d",&t); while(t--) {scanf ("%d%s", &n,a+1); scanf ("%d%s", &mm,b+1); printf ("%d\n", Solve (n,mm)); }}intMain () {//freopen ("A.in.cpp", "R", stdin);Init (); Input (); return 0;}voidInit () {m.clear (); m['A'] =1; m['C'] =2; m['G'] =3; m['T'] =4; Memset (g,0,sizeof(g)); for(inti =1; I <=4; i++) {G[i][i]=5; } g[1][5] = g[5][1] = -3; g[2][5] = g[5][2] = -4; g[3][5] = g[5][3] = -2; g[4][5] = g[5][4] = -1; g[1][2] = g[2][1] = g[1][4] = g[4][1] = -1; g[1][3] = g[3][1] = g[2][4] = g[4][2] = g[3][4] = g[4][3] = -2; g[2][3] = g[3][2] = -3;}
Uvalive 2324Human Gene Functions (Dynamic planning)