Advertisement
Josif_tepe

Untitled

Jun 8th, 2025
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. string a, b;
  5. int dp[1001][1001];
  6. int rec(int i, int j) {
  7.     if(i < 0 or j < 0) {
  8.         return 0;
  9.     }
  10.     if(dp[i][j] != -1) {
  11.         return dp[i][j];
  12.     }
  13.     int res = 0;
  14.     res = max(res, rec(i - 1, j));
  15.     res = max(res, rec(i, j - 1));
  16.    
  17.     if(a[i] == b[j]) {
  18.         res = max(res, rec(i - 1, j - 1) + 1);
  19.     }
  20.     dp[i][j] = res;
  21.     return res;
  22. }
  23. int main()
  24. {
  25.     cin >> a >> b;
  26.     memset(dp, -1, sizeof dp);
  27.     cout << rec(a.size() - 1, b.size() - 1) << endl;
  28.    
  29.     return 0;
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement