Advertisement
Josif_tepe

Untitled

May 28th, 2024
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. class Solution {
  2. public:
  3. string a, b;
  4. int dp[1005][1005];
  5. const int INF = 2e8;
  6. int rec(int i, int j) {
  7.     if(i == -1) {
  8.         return 0;
  9.     }
  10.     if(j == -1) {
  11.         return 0;
  12.     }
  13.     if(dp[i][j] != -1) {
  14.         return dp[i][j];
  15.     }
  16.     int res = -INF;
  17.     if(a[i] == b[j]) {
  18.         res = max(res, rec(i - 1, j - 1) + 1);
  19.     }
  20.     res = max(res, rec(i - 1, j));
  21.     res = max(res, rec(i, j - 1));
  22.     return dp[i][j] = res;
  23. }
  24.     int longestCommonSubsequence(string text1, string text2) {
  25.         a = text1;
  26.         b = text2;
  27.     memset(dp, -1, sizeof dp);
  28.         return rec(a.size() - 1, b.size() - 1);
  29.     }
  30. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement