#include <iostream> #include <algorithm> using namespace std; const int N = 1010; int n, m; char a[N], b[N]; int f[N][N];//a的前N个去匹配b的前N个需要编辑的最小操作 int main() { scanf("%d%s", &n, a + 1); scanf("%d%s", &m, b + 1); for (int i = 0; i <= m; i ++ ) f[0][i] = i;//a的前0个字母去匹配b的前i个字母,只能加 for (int i = 0; i <= n; i ++ ) f[i][0] = i;//a的前i个字母去匹配b的前0个字母,只能减 for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ ) {
// 删除操作 增 f[i][j] = min(f[i - 1][j] + 1, f[i][j - 1] + 1);
// 改 if (a[i] == b[j]) f[i][j] = min(f[i][j], f[i - 1][j - 1]); else f[i][j] = min(f[i][j], f[i - 1][j - 1] + 1); } printf("%d\n", f[n][m]); return 0; }