Skip to content

Instantly share code, notes, and snippets.

@musou1500
Last active May 30, 2020 11:30
Show Gist options
  • Save musou1500/e737af4b9fe151b41e44e7431076657b to your computer and use it in GitHub Desktop.
Save musou1500/e737af4b9fe151b41e44e7431076657b to your computer and use it in GitHub Desktop.
int LcsLen(string &s, string &t, vector<vector<int>> &memo, int i, int j) {
if (i < 0 || j < 0) {
return 0;
}
if (memo[i][j] < 0) {
if (s[i] == t[j]) {
memo[i][j] = LcsLen(s, t, memo, i - 1, j - 1) + 1;
} else {
memo[i][j] =
max(LcsLen(s, t, memo, i - 1, j), LcsLen(s, t, memo, i, j - 1));
}
}
return memo[i][j];
}
string Lcs(string &s, string &t, vector<vector<int>> &memo, int i, int j) {
if (i < 0 || j < 0) {
return "";
}
if (s[i] == t[j]) {
return Lcs(s, t, memo, i - 1, j - 1) + s[i];
}
if (LcsLen(s, t, memo, i, j - 1) < LcsLen(s, t, memo, i - 1, j)) {
return Lcs(s, t, memo, i - 1, j);
} else {
return Lcs(s, t, memo, i, j - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment