【Algorithm】字符串编辑距离(Levenshtein距离)C++算法实现
2017-08-09 17:02
417 查看
算法实现比较简单,但算法原理不明白,有空了再研究一下。
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]; }
相关文章推荐
- levenshtein字符串编辑距离算法
- C++实现两个字符串之间的Levenshtein Distance(编辑距离)
- 【动态规划】字符串编辑距离(Levenshtein距离)算法
- Levenshtein字符串编辑距离算法
- 1.交通聚类:编辑距离 (Levenshtein距离)Java实现
- 字符串编辑距离的几种实现
- 字符串反转-编辑距离-约瑟夫环的实现
- 字符串编辑距离(C递归实现)
- 【动态规划】字符串最小编辑距离Java实现
- 字符串编辑距离(Levenshtein距离)算法
- Python实现求两个字符串的最短编辑距离
- 字符串编辑距离(Levenshtein距离)算法
- java实现编辑距离算法,计算字符串相似度
- 用C#实现字符串相似度算法(编辑距离算法 Levenshtein Distance)
- 求字符串编辑距离的递推和递归实现
- 字符串相关算法(编辑距离,最大公共字串)C#实现版
- 字符串编辑距离的c#实现
- 动态规划(最小字符串编辑距离实现)
- 字符串编辑距离
- 字符串的编辑距离