Advertisement
wym1111

Untitled

Jun 10th, 2024
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5. #define endl '\n'
  6. const int N = 2e3 + 10;
  7.  
  8. string s1, s2;
  9.  
  10. int dp[N][N];
  11.  
  12. void solve() {
  13.     cin >> s1 >> s2;
  14.     int n = s1.size(), m = s2.size();
  15.     s1 = ' ' + s1;
  16.     s2 = ' ' + s2;
  17.     memset(dp, 0x3f, sizeof dp);
  18.    
  19.     for (int i = 0; i <= max(n, m); i ++) {
  20.         dp[0][i] = i;
  21.         dp[i][0] = i;
  22.     }
  23.     for (int i = 1; i <= n; i ++) {
  24.         for (int j = 1; j <= m; j ++) {
  25.             if (s1[i] == s2[j]) {
  26.                 dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
  27.             } else {
  28.                 dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
  29.                 dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
  30.                 dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1);
  31.             }
  32.         }
  33.     }
  34.     int ans = n + m;
  35.     for (int i = 1; i <= n; i ++) {
  36.         ans = min(ans, dp[i][m] + n - i);
  37.     }
  38.     cout << ans << endl;
  39. }
  40.  
  41. signed main() {
  42.     ios::sync_with_stdio(false);
  43.     cin.tie(nullptr);
  44.    
  45.     int _ = 1;
  46. //  cin >> _;
  47.     while(_--)
  48.         solve();
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement