Main topic:
Give three strings A, B, C, the longest string d, so that D is the common sub-sequence of a A, and C is a continuous substring of d.
Problem Solving Ideas:
Preprocessing the forward and inverse common sub-sequences of a, B string. It then enumerates the starting and ending points of the string C that appear in A, B, and ans is the longest common subsequence to the left of the starting point plus the length of the string C plus the longest common subsequence to the right of the starting point.
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <set> #include <map> #include <stack># Define LL long longusing namespace Std;const int maxn = + 10;char S1[MAXN], S2[MAXN], S3[maxn];int DP1[MAXN][MAXN], D P2[maxn][maxn];int LEFT1[MAXN], RIGHT1[MAXN], LEFT2[MAXN], Right2[maxn];int main () {int T, kcase = 1; scanf ("%d", &t); while (t--) {scanf ("%s%s%s", &s1, &s2, &S3); memset (DP1, 0, sizeof (DP1)); memset (DP2, 0, sizeof (DP2)); int len1 = strlen (S1), len2 = strlen (s2), Len3 = strlen (S3); for (int i=1;i<=len1;i++) {for (int j=1;j<=len2;j++) {if (s1[i-1] = = S2[j -1]) dp1[i][j] = dp1[i-1][j-1] + 1; else dp1[i][j] = max (Dp1[i-1][j], dp1[i][j-1]); }} for (int i=len1-1;i>=0;i--){for (int j=len2-1;j>=0;j--) {if (s1[i] = = S2[j]) dp2[i][j] = d P2[I+1][J+1] + 1; else dp2[i][j] = max (Dp2[i+1][j], dp2[i][j+1]); }} int m1 = 0; for (int i=0;i<len1;i++) {int k = 0; if (s1[i] = = S3[k]) {for (int j=i;j<len1;j++) {if (s1[j] = = S3[k]) {k++; if (k = = Len3) {LEFT1[M1] = i; right1[m1++] = j; Break }}}}} int m2 = 0; for (int i=0;i<len2;i++) {int k = 0; if (s2[i] = = S3[k]) {for (int j=i;j<len2;j++) {if (s2[j] = = S3[K]) {k++; if (k = = Len3) {left2[m2] = i; right2[m2++] = j; Break }}}}} int ans = 0; for (int i=0;i<m1;i++) {for (int j=0;j<m2;j++) {ans = max (ans, dp1[left1 [I]] [Left2[j]] + Len3 + dp2[right1[i]+1][right2[j]+1]); }} printf ("Case #%d:%d\n", kcase++, ans); } return 0;;}
HDU 4681 String (DP)