Untitled
unknown
plain_text
2 years ago
898 B
2
Indexable
class Solution { public: string shortestCommonSupersequence(string a, string b) { int n, m; n = a.length(); m = b.length(); vector<vector<string>> dp(n+1, vector<string>(m+1, "")); for (int i=0; i<m; ++i) dp[n][i] = b.substr(i); for (int j=0; j<n; ++j) dp[j][m] = a.substr(j); for (int i=n-1; i>=0; --i){ for (int j=m-1; j>=0; --j){ if (a[i] == b[j]){ dp[i][j] = a[i] + dp[i+1][j+1]; } else{ int l1 = dp[i+1][j].length(); int l2 = dp[i][j+1].length(); if (l1 < l2) dp[i][j] = a[i] + dp[i+1][j]; else dp[i][j] = b[j] + dp[i][j+1]; } } } return dp[0][0]; } };
Editor is loading...