You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
717 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
int f[N][N];
char a[N], b[N];
int al, bl;
/*
测试用例:
abcd
ebde
答案3
*/
int dfs(int i, int j) {
if (i == 0) return j;
if (j == 0) return i;
if (~f[i][j]) return f[i][j];
//增加、删除
f[i][j] = min(dfs(i - 1, j) + 1, dfs(i, j - 1) + 1);
//修改
if (a[i] == b[j])
return f[i][j] = min(f[i][j], dfs(i - 1, j - 1));
else
return f[i][j] = min(f[i][j], dfs(i - 1, j - 1) + 1);
}
int main() {
memset(f, -1, sizeof f);
cin >> a + 1 >> b + 1;
al = strlen(a + 1), bl = strlen(b + 1);
printf("%d\n", dfs(al, bl));
return 0;
}