// DP dynamic planning, deformation of LCS problems. Based on the classic LCS model, we use DP [I, j] to represent the optimal values matching I and j. the final answer is DP [len1] [len2] <br/> // obviously there are two situations: <br/> // 1. S1 [I] = S2 [J, state transfer is naturally DP [I] [J] = 5 + dp [I-1] [J-1] <br/> // II. S1 [I]! = S2 [J]. There are several matching cases: <br/> // 1. Match S1 [I] and S2 [J] <br/> // 2. match them with spaces, so there are two matching schemes <br/> // DP [I] [J] = max (match + dp [I-1] [J], match + dp [I] [J-1]), either S1 [I] matches with a space, either S2 [J] is matched with a space <br/> // you can draw a picture for the second case. <br/> //.... A .... a... A _ <br/> //.... c... C _.... c <br/> // The last is the border and initialization problem ~ The maximum value is required, then the initialized DP is the boundary of-INF <br/> // during the initialization of DP [0] [I] and DP [I] [0, match all of them with spaces. <br/> # include <iostream> <br/> # include <cstring> <br/> # define INF 1000000000 <br/> using namespace STD; <br/> int match [128] [128], T; <br/> int DP [105] [105]; <br/> char S1 [105], s2 [105]; <br/> int len1, len2; <br/> void Init () // array of matching scores <br/>{< br/> match ['a'] ['C'] = match ['C'] ['a'] =- 1; <br/> match ['a'] ['T'] = match ['T'] ['a'] =-1; <br /> Match ['a'] ['G'] = match ['G'] ['a'] =-2; <br/> match ['C'] ['T'] = match ['T'] ['C'] =-2; <br/> match ['C'] ['G'] = match ['G'] ['C'] =-3; <br/> match ['T'] ['G'] = match ['G'] ['T'] =-2; <br/> match ['a'] [''] = match [''] ['a'] =-3; <br/> match ['C'] [''] = match [''] ['C'] =-4; <br/> match ['T'] [''] = match [''] ['T'] =-1; <br/> match ['G'] [''] = match [''] ['G'] =-2; <br/>}< br/> int main () <br/>{< br/> Init (); <Br/> // freopen ("in.txt", "r", stdin); <br/> scanf ("% d", & T ); <br/> while (t --) <br/> {<br/> scanf ("% d % s", & len1, S1 + 1 ); <br/> scanf ("% d % s", & len2, S2 + 1); </P> <p> for (INT I = 0; I <= len1; + + I) <br/> for (Int J = 0; j <= len2; ++ J) <br/> DP [I] [J] =-INF; <br/> DP [0] [0] = 0; <br/> for (INT I = 1; I <= len2; ++ I) <br/> DP [0] [I] = match [S2 [I] [''] + dp [0] [I-1]; <br/> for (INT I = 1; I <= len1; ++ I) <br/> DP [I] [0] = match [S1 [I] [' '] + Dp [I-1] [0]; <br/> // The above rows are initialization boundary <br/> for (INT I = 1; I <= len1; ++ I) // DP Process <br/>{< br/> for (Int J = 1; j <= len2; ++ J) <br/>{< br/> If (S1 [I]! = S2 [J]) <br/>{< br/> DP [I] [J] = max (DP [I] [J], DP [I-1] [J-1] + match [S1 [I] [S2 [J]); <br/> DP [I] [J] = max (DP [I] [J], max (DP [I-1] [J] + match [S1 [I] [''], DP [I] [J-1] + match [S2 [J] ['']); <br/>}< br/> elsedp [I] [J] = max (DP [I] [J], 5 + dp [I-1] [J-1]); <br/>}< br/> printf ("% d/N", DP [len1] [len2]); <br/>}< br/>}