Advertisement
Josif_tepe

Untitled

Dec 30th, 2022
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. using namespace std;
  5. string s1, s2;
  6. int dp[5005][5005];
  7.  
  8. int main() {
  9.     cin >> s1 >> s2;
  10.     int n = (int) s1.size();
  11.     int m = (int) s2.size();
  12.    
  13.     for(int i = 0; i <= n; i++) {
  14.         for(int j = 0; j <= m; j++) {
  15.             dp[i][j] = 2e9;
  16.         }
  17.     }
  18.     for(int i = 0; i <= n; i++) {
  19.         for(int j = 0; j <= m; j++) {
  20.             if(i == 0) {
  21.                 dp[i][j] = j;
  22.                 continue;
  23.             }
  24.             if(j == 0) {
  25.                 dp[i][j] = i;
  26.                 continue;
  27.             }
  28.             if(s1[i - 1] == s2[j - 1]) {
  29.                 dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
  30.             }
  31.             dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
  32.             dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
  33.             dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1);
  34.         }
  35.     }
  36.     cout << dp[n][m] << endl;
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement