Thinking:
solving the problem of all the sub-s1 [1-i] and s2 [1-j]. s1 [1-i] denotes the range of 1 ~ s1 sub-string i, s2 [1-j] denotes the range s2 1-j substrings.
For example:
#define N1 8
#define N2 9
int s1[N1+1] = {0,1,3,4,5,6,7,7,8 };
int s2[N2+1] = {0,3,5,7,4,8,6,7,8,2 };
All of its sub-problems as follows:
S1 [~. 1. 1], S2 [1-1 of], which required the longest common subsequence length
s1 [1 ~ 1], s2 [1-2], which find the longest common subsequence length
s1 [1 ~ 1], s2 [1-2], find the longest common subsequence length
s1 [1 ~ 1], s2 [1-3], find the longest common subsequence length
s1 [1 ~ 1] , s2 [1-4], which find the longest common subsequence length
s1 [1 ~ 1], s2 [1-5], which find the longest common subsequence length
s1 [1 ~ 1], s2 [1-6 ], find the longest common subsequence length
s1 [1 ~ 1], s2 [1-7], which find the longest common subsequence length
s1 [1 ~ 1], s2 [1-8], the longest seek common subsequence length
s1 [1 ~ 1], s2 [1-9], which find the longest common subsequence length
s1 [1 ~ 2], s2 [1-1], which find the longest common subsequence length
S1 [ 1 ~ 2], s2 [1-2 ], which find the longest common subsequence length
s1 [1 ~ 2], s2 [1-3], which find the longest common subsequence length
s1 [1 ~ 2], s2 [1-4], find the longest common subsequence length
s1 [1 ~ 2], s2 [1-5], which find the longest common subsequence length
s1 [1 ~ 2], s2 [1-6], seeking the longest common subsequence length
s1 [1 ~ 2], s2 [1-7], which find the longest common subsequence length
s1 [1 ~ 2], s2 [1-8], seeking the longest common sequence length
s1 [1 ~ 2], s2 [1-9] Seeking the longest common subsequence length
s1 [1 ~ 3], s2 [1-1], which find the longest common subsequence length
s1 [1 ~ 3], s2 [1-2], which find the longest common subsequence length
s1 [1 ~ 3], s2 [1-3], which find the longest common subsequence length
s1 [1 ~ 3] , s2 [1-4], which find the longest common subsequence length
s1 [1 ~ 3], s2 [1-5], which find the longest common subsequence length
s1 [1 ~ 3], s2 [1-6 ], find the longest common subsequence length
s1 [1 ~ 3], s2 [1-7], which find the longest common subsequence length
s1 [1 ~ 3], s2 [1-8], the longest seek common subsequence length
s1 [1 ~ 3], s2 [1-9], which find the longest common subsequence length
s1 [1 ~ 4], s2 [1-1], which find the longest common subsequence length
S1 [ 1 ~ 4], s2 [1-2 ], which find the longest common subsequence length
s1 [1 ~ 4], s2 [1-3], which find the longest common subsequence length
s1 [1 ~ 4], s2 [1-4], find the longest common subsequence length
s1 [1 ~ 4], s2 [1-5], which find the longest common subsequence length
s1 [1 ~ 4], s2 [1-6], seeking the longest common subsequence length
s1 [1 ~ 4], s2 [1-7], which find the longest common subsequence length
s1 [1 ~ 4], s2 [1-8], seeking the longest common sequence length
s1 [1 ~ 4], s2 [1-9], which find the longest common subsequence length
s1 [1 ~ 5], s2 [1-1], which find the longest common subsequence length
s1 [1 ~ 5], s2 [1-2], find the longest common subsequence Length
s1 [1 ~ 5], s2 [1-3], which find the longest common subsequence length
s1 [1 ~ 5], s2 [1-4], which find the longest common subsequence length
s1 [1 ~ 5], s2 [1-5], which find the longest common subsequence length
s1 [1 ~ 5] , s2 [1-6], which find the longest common subsequence length
s1 [1 ~ 5], s2 [1-7], which find the longest common subsequence length
s1 [1 ~ 5], s2 [1-8 ], find the longest common subsequence length
s1 [1 ~ 5], s2 [1-9], which find the longest common subsequence length
s1 [1 ~ 6], s2 [1-1], the longest seek common subsequence length
s1 [1 ~ 6], s2 [1-2], which find the longest common subsequence length
s1 [1 ~ 6], s2 [1-3], which find the longest common subsequence length
S1 [ 1 ~ 6], s2 [1-4 ], which find the longest common subsequence length
s1 [1 ~ 6], s2 [1-5], which find the longest common subsequence length
s1 [1 ~ 6], s2 [1-6], find the longest common subsequence length
s1 [1 ~ 6], s2 [1-7], which find the longest common subsequence length
s1 [1 ~ 6], s2 [1-8], seeking the longest common subsequence length
s1 [1 ~ 6], s2 [1-9], which find the longest common subsequence length
s1 [1 ~ 7], s2 [1-1], seeking the longest common sequence length
s1 [1 ~ 7], s2 [1-2], which find the longest common subsequence length
s1 [1 ~ 7], s2 [1-3], which find the longest common subsequence length
s1 [1 ~ 7], s2 [1-4], find the longest common subsequence Length
s1 [1 ~ 7], s2 [1-5], which find the longest common subsequence length
s1 [1 ~ 7], s2 [1-6], which find the longest common subsequence length
s1 [1 ~ 7], s2 [1-7], which find the longest common subsequence length
s1 [1 ~ 7] , s2 [1-8], which find the longest common subsequence length
s1 [1 ~ 7], s2 [1-9], which find the longest common subsequence length
s1 [1 ~ 8], s2 [1-1 ], find the longest common subsequence length
s1 [1 ~ 8], s2 [1-2], which find the longest common subsequence length
s1 [1 ~ 8], s2 [1-3], the longest seek common subsequence length
s1 [1 ~ 8], s2 [1-4], which find the longest common subsequence length
s1 [1 ~ 8], s2 [1-5], which find the longest common subsequence length
S1 [ 1 ~ 8], s2 [1-6 ], which find the longest common subsequence length
s1 [1 ~ 8], s2 [1-7], which find the longest common subsequence length
s1 [1 ~ 8], s2 [1-8], find the longest common subsequence length
s1 [1 ~ 8], s2 [1-9], which find the longest common subsequence length
The answer to all sub-problems are present in the two-dimensional array m.
Reverse two-dimensional array of output answers, like specific process directly see the code.
Code is implemented as follows:
#include <iostream>
using namespace std;
int main()
{
#define N1 8
#define N2 9
int s1[N1+1] = {0,1,3,4,5,6,7,7,8 };
int s2[N2+1] = {0,3,5,7,4,8,6,7,8,2 };
int m[9][10] = { 0 };
for (int i = 1; i <= N1; i++) {
for (int j = 1; j <= N2; j++) {
if (s1[i] == s2[j]) {
m[i][j] = m[i-1][j-1] + 1;
}else{
if (m[i - 1][j] > m[i][j - 1]) {
m[i][j] = m[i-1][j];
}
else {
m[i][j] = m[i][j-1];
}
}
}
}
// 输出数组m
for (int i = 0; i <= N1; i++) {
for (int j = 0; j <= N2; j++) {
cout << m[i][j] << " ";
}
putchar(10);
}
// 输出答案
cout << "倒序输出答案:";
int i = N1, j = N2;
while (i >= 1 && j >= 1) {
if (s1[i] == s2[j]) {
cout << s1[i] << " ";
i--; j--;
}
else {
if (m[i - 1][j] > m[i][j - 1]) {
i--;
}
else {
j--;
}
}
}
putchar(10);
return 0;
}
Reference: https: //blog.csdn.net/UncleJokerly/article/details/79701073