Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int memo[1005][1005];
- int longestCommonSubsequence(string text1, string text2) {
- memset(memo, -1, sizeof(memo));
- return solve(0, 0, text1, text2);
- }
- int solve(int i, int j, string &s, string &t){
- if(i == s.size() || j == t.size())
- return 0;
- int &ans = memo[i][j];
- if(~ans) return ans;
- if(s[i] == t[j])
- ans = solve(i + 1, j + 1, s, t) + 1;
- else
- ans = max(solve(i + 1, j, s, t), solve(i, j + 1, s, t));
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement