AcWing 902. 最短编辑距离 C
原题链接
简单
作者:
LaChimere
,
2021-06-05 16:41:51
,
所有人可见
,
阅读 241
C
#include <stdio.h>
#define MAXSIZE 1010
#define min(a,b) ((a)<(b)?(a):(b))
int n, m, dp[MAXSIZE][MAXSIZE];
char a[MAXSIZE], b[MAXSIZE];
int main() {
scanf("%d%s%d%s", &n, a+1, &m, b+1);
for (int i = 1; i < MAXSIZE; ++i) {
dp[i][0] = dp[0][i] = i;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1;
if (a[i] == b[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]+1);
}
}
}
printf("%d\n", dp[n][m]);
return 0;
}