Josif_tepe

Untitled

Oct 29th, 2025
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <cstring>
  6. #include <map>
  7. using namespace std;
  8. typedef long long ll;
  9.  
  10. const int maxn = 105;
  11. const ll INF = 1e16;
  12. string a, b;
  13.  
  14. int dp[1005][1005];
  15. int rec(int i, int j) {
  16.     if(i < 0 or j < 0) {
  17.         return 0;
  18.     }
  19.    
  20.     if(dp[i][j] != -1) {
  21.         return dp[i][j];
  22.     }
  23.     int res = 0;
  24.    
  25.     if(a[i] == b[j]) {
  26.         res = max(res, rec(i - 1, j - 1) + 1);
  27.     }
  28.    
  29.     res = max(res, rec(i - 1, j));
  30.     res = max(res, rec(i, j - 1));
  31.    
  32.     dp[i][j] = res;
  33.     return res;
  34. }
  35. int main()
  36. {
  37.     memset(dp, -1, sizeof dp);
  38.     cin >> a >> b;
  39.    
  40.     cout << rec((int) a.size() - 1, (int) b.size() - 1) << endl;
  41.  
  42. return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment