Josif_tepe

Untitled

Jan 30th, 2026
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <queue>
  4. using namespace std;
  5. const int INF = 1e9;
  6. const int maxn = 5005;
  7. string A, B;
  8. int dp[maxn][maxn];
  9. int rec(int i, int j) {
  10.     if(i < 0) {
  11.         return j + 1;
  12.     }
  13.     if(j < 0) {
  14.         return i + 1;
  15.     }
  16.     if(dp[i][j] != -1) {
  17.         return dp[i][j];
  18.     }
  19.     int res = INF;
  20.    
  21.     if(A[i] == B[j]) {
  22.         res = min(res, rec(i - 1, j - 1));
  23.     }
  24.    
  25.     res = min(res, rec(i - 1, j) + 1);
  26.     res = min(res, rec(i, j - 1) + 1);
  27.     res = min(res, rec(i - 1, j - 1) + 1);
  28.    
  29.     dp[i][j] = res;
  30.     return res;
  31. }
  32. int main() {
  33.     for(int i = 0; i < maxn; i++) {
  34.         for(int j = 0; j < maxn; j++) {
  35.             dp[i][j] = -1;
  36.         }
  37.     }
  38.     cin >> A >> B;
  39.    
  40.     cout << rec(A.size() - 1, B.size() - 1) << endl;
  41.     return 0;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment