Last active
May 30, 2020 11:30
-
-
Save musou1500/e737af4b9fe151b41e44e7431076657b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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