6void Print(
int trace[20][20],
int m,
int n,
string a) {
7 if (m == 0 || n == 0) {
10 if (trace[m][n] == 1) {
11 Print(trace, m - 1, n - 1, a);
13 }
else if (trace[m][n] == 2) {
14 Print(trace, m - 1, n, a);
15 }
else if (trace[m][n] == 3) {
16 Print(trace, m, n - 1, a);
20int lcs(
string a,
string b) {
21 int m = a.length(), n = b.length();
22 std::vector<std::vector<int> > res(m + 1, std::vector<int>(n + 1));
26 for (
int i = 0; i < m + 1; i++) {
27 for (
int j = 0; j < n + 1; j++) {
33 for (
int i = 0; i < m + 1; ++i) {
34 for (
int j = 0; j < n + 1; ++j) {
35 if (i == 0 || j == 0) {
40 else if (a[i - 1] == b[j - 1]) {
41 res[i][j] = 1 + res[i - 1][j - 1];
45 if (res[i - 1][j] > res[i][j - 1]) {
46 res[i][j] = res[i - 1][j];
50 res[i][j] = res[i][j - 1];
57 Print(trace, m, n, a);