C语言多种方法求解字符串编辑距离问题编辑距离:通过插入、删除、替换一个字符(和交换相邻字符)的操作,使得字符串A和字符串B相同,而最少的操作次数就是编辑距离。如字符串abcd和aca的距离是2
/* 递归搜索 */
int calDistance1(char *ptrX, int xbeg, int xend, char *ptrY, int ybeg, int yend)
{
if(xbeg > xend)
{
if(ybeg > yend)
return 0;
else
return yend - ybeg + 1;
}
if(ybeg > yend)
{
if(xbeg > xend)
return 0;
else
return xend - xbeg + 1;
}
if(ptrX[xend] == ptrY[yend])
{
return calDistance1(ptrX,xbeg,xend-1,ptrY,ybeg,yend-1);
}else
{
int t1 = calDistance1(ptrX,xbeg,xend-1,ptrY,ybeg,yend);
int t2 = calDistance1(ptrX,xbeg,xend,ptrY,ybeg,yend-1);
int t3 = calDistance1(ptrX,xbeg,xend-1,ptrY,ybeg,yend-1);
t1 = t1 < t2 ? t1 : t2;
return (t1 < t3 ? t1 : t3) + 1;
}
}
开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C语言多种方法求解字符串编辑距离问题!
本文介绍了一种使用C语言实现的递归方法来计算两个字符串之间的编辑距离。编辑距离是指通过最少的插入、删除或替换操作使两个字符串相等所需的步骤数。文章提供了具体的C语言代码示例。

1176

被折叠的 条评论
为什么被折叠?



