2
string LCS(string str1, string str2, int m, int n) {5
for (int i = 0; i < m + 1; ++i) {6
for (int j = 0; j < n + 1; ++j) {7
if (i == 0 || j == 0) t[i][j] = 0;11
for (int i = 1; i < m + 1; ++i) {12
for (int j = 1; j < n + 1; ++j) {13
if (str1[i - 1] == str2[j - 1])14
t[i][j] = 1 + t[i - 1][j - 1];16
t[i][j] = max(t[i - 1][j], t[i][j - 1]);20
while (i > 0 && j > 0) {21
if (str1[i - 1] == str2[j - 1]) {22
ans.push_back(str1[i - 1]);27
else if (t[i][j - 1] > t[i - 1][j]) {28
ans.push_back(str2[j - 1]);33
ans.push_back(str1[i - 1]);38
ans.push_back(str1[i - 1]);42
ans.push_back(str2[j - 1]);45
reverse(ans.begin(), ans.end());50
string shortestCommonSupersequence(string str1, string str2) {51
int m = str1.length();52
int n = str2.length();54
return LCS(str1, str2, m, n);