算法实现比较简单,但算法原理不明白,有空了再研究一下。
unsigned LevenshteinDistance(const string& s1, const string& s2) { if (s1.empty()) { return (unsigned)s2.size(); } if (s2.empty()) { return (unsigned)s1.size(); } unsigned row = (unsigned)s1.size() + 1; unsigned col = (unsigned)s2.size() + 1; auto_ptr<unsigned> apBuf(new unsigned[row * col]); unsigned* pBuf = apBuf.get(); for (unsigned i=0; i < row; ++i) { pBuf[i * col] = i; } for (unsigned i=0; i < col; ++i) { pBuf[i] = i; } for (unsigned i=1; i < row; ++i) { for (unsigned j = 1; j < col; ++j) { unsigned temp = (s1[i-1] == s2[j-1]) ? 0 : 1; pBuf[i * col + j] = min( min( pBuf[(i-1) * col + j] + 1, pBuf[i * col + j - 1] + 1 ), (pBuf[(i -1 ) * col + j - 1] + temp) ); } } // dump buf for (unsigned i=0; i < row; ++i) { for (unsigned j= 0; j < col; ++j) { cout << pBuf[i * col + j] << " "; } cout << endl; } return pBuf[row * col - 1]; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· 对象命名为何需要避免'-er'和'-or'后缀
· JDK 24 发布,新特性解读!
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· SQL Server如何跟踪自动统计信息更新?