Question:
For two strings consisting of four characters: A, C, T, and G, you can add-to the two strings so that the two strings are of the same length.
Each two-character match has a value. The two-character cannot match.
Solution:
This is converted into a bare longest public substring, but requires the same length during matching.
Dp [I] [j] indicates the maximum value that can be reached when the first I character of the first string matches the first j character of the second string.
During initialization, note that dp [0] [j] and dp [j] [0] cannot be zero, which is the sum of matched characters and.
Code:
<SPAN style = "FONT-SIZE: 14px "> # include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <set> # include <stack> # include <list> # include <queue> # define eps 1e-6 # define INF 0x1f1f1f1f # define PI acos (-1.0) # define ll _ int64 # define lson l, m, (rt <1) # define rson m + 1, r, (rt <1) | 1 // # pragma comment (linker, "/STACK: 1024000000,1024000000") using namespace std;/* freopen ("data. in "," r ", stdin); freopen (" data. out "," w ", stdout); */# define Maxn 110map <char, int> myp; int mar [5] [5] = {5,-1, -2,-1,-3}, {-1,-3,-2,-4}, {-2,-3, 5,-2,-2 }, {-1,-2,-2, 5,-1}, {-3,-4,-2,-1,-INF }}; // store the value table char sa1 [Maxn], sa2 [Maxn]; int a [Maxn], B [Maxn]; int dp [Maxn] [Maxn]; int main () {myp ['a'] = 0, myp ['C'] = 1, myp ['G'] = 2, myp ['T'] = 3, myp ['-'] = 4; // map characters and tables to int t, len1, len2; scanf ("% d", & t); while (t --) {scanf ("% d % s", & len1, sa1 + 1); for (int I = 1; I <= len1; I ++) a [I] = myp [sa1 [I]; // converts it to the corresponding number scanf ("% d % s", & len2, sa2 + 1 ); for (int I = 1; I <= len2; I ++) B [I] = myp [sa2 [I]; memset (dp,-INF, sizeof (dp); dp [0] [0] = 0; for (int I = 1; I <= len1; I ++) // note that the dp [I] [0] = dp [I-1] [0] + mar [a [I] [4] is matched with-during initialization; // printf ("I: % d j: % d \ n", I, 0, dp [I] [0]); for (int j = 1; j <= len2; j ++) dp [0] [j] = dp [0] [J-1] + mar [B [j] [4]; // printf ("I: % d j: % d \ n", 0, j, dp [0] [j]); // putchar ('\ n'); for (int I = 1; I <= len1; I ++) for (int j = 1; j <= len2; j ++) {int Mx = dp [I] [j]; Mx = max (Mx, dp [I-1] [J-1] + mar [a [I] [B [j]); // I-j match Mx = max (Mx, max (dp [I-1] [j] + mar [a [I] [4], dp [I] [J-1] + mar [B [j] [4]); // (j and I-1, I and-) matching (I and J-1, -Match Mx = max (Mx, dp [I-1] [J-1] + mar [a [I] [4] + mar [B [I] [4]); // both match-dp [I] [j] = Mx; // printf ("I: % d j: % d \ n", I, j, dp [I] [j]);} printf ("% d \ n", dp [len1] [len2]);} return 0 ;}</SPAN>