/*==========================================================*\
| 编辑距离:
| d[i, j] = minimum(
| d[i-1, j] + 1, 在b上j位置删除字符(或在a上i-1位置插入字符)
| d[i, j - 1] + 1, 在b上j-1位置插入字符(或在a上i位置删除字符)
| d[i-1, j - 1] + cost 替换操作)
\*==========================================================*/
int ans[2005][2005];
int main()
{
char a[2005], b[2006];
int x, y;
while (scanf("%s%s", a, b) == 2) {
memset(ans, 0, sizeof(ans));
x = strlen(a);
y = strlen(b);
int i, j, cost;
for (i = 0; i <= x; ++i) ans[i][0] = i;
for (i = 0; i <= y; ++i) ans[0][i] = i;
for (i = 0; i < x; ++i) {
for (j = 0; j < y; ++j) {
cost = (a[i] != b[j]);
ans[i + 1][j + 1] = MY_MIN(ans[i][j] + cost, MY_MIN(ans[i][j + 1] + 1, ans[i + 1][j] + 1));
}
}
printf("%d\n", ans[x][y]);
}
return 0;
}
POJ-3356(编辑距离)(AGTC)
最新推荐文章于 2023-04-14 02:21:45 发布