Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- #define endl '\n'
- const int N = 2e3 + 10;
- string s1, s2;
- int dp[N][N];
- void solve() {
- cin >> s1 >> s2;
- int n = s1.size(), m = s2.size();
- s1 = ' ' + s1;
- s2 = ' ' + s2;
- memset(dp, 0x3f, sizeof dp);
- for (int i = 0; i <= max(n, m); i ++) {
- dp[0][i] = i;
- dp[i][0] = i;
- }
- for (int i = 1; i <= n; i ++) {
- for (int j = 1; j <= m; j ++) {
- if (s1[i] == s2[j]) {
- dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
- } else {
- dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
- dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
- dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1);
- }
- }
- }
- int ans = n + m;
- for (int i = 1; i <= n; i ++) {
- ans = min(ans, dp[i][m] + n - i);
- }
- cout << ans << endl;
- }
- signed main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- int _ = 1;
- // cin >> _;
- while(_--)
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement