// 求两个字符串的编辑距离
int editMinDistance( const char *s1, const char *s2 )
{
int dist = 0;
if( NULL == s1 && NULL == s2 )
{
return dist;
}
if( NULL == s1 )
{
dist = strlen(s2);
return dist;
}
else if( NULL == s2 )
{
dist = strlen(s1);
return dist;
}
int len1 = strlen(s1);
int len2 = strlen(s2);
if( 0 == len1 && 0 == len2 )
{
dist = 0;
return dist;
}
else if( 0 == len1 )
{
return len2;
}
else if( 0 == len2 )
{
return len1;
}
if( s1[0] == s2[0] )
{
dist = editMinDistance( s1 + 1, s2 + 1 );
}
else
{
int d1 = 1 + editMinDistance( s1 + 1, s2 + 1 );
int d2 = 1 + editMinDistance( s1, s2 + 1 );
int d3 = 1 + editMinDistance( s1 + 1, s2 );
int max = d1 > d2 ? d2 : d1;
dist = max > d3 ? d3 : max;
}
return dist;
}
=================================================
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int minValue( int v1, int v2, int v3 )
{
if( v1 <= v2 && v1 <= v3 )
{
return v1;
}
if( v2 <= v1 && v2 <= v3 )
{
return v2;
}
if( v3 <= v1 && v3 <= v2 )
{
return v3;
}
}
// 求两个字符串的编辑距离
// 编辑距离,又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。
// From: 《王道-程序员求职宝典P192》
int calStringDistance( string X, string Y )
{
int result = 0;
int len1 = X.length();
int len2 = Y.length();
if( 0 == len1 )
{
result = len2;
return result;
}
if( 0 == len2 )
{
result = len1;
return result;
}
int **p = new int *[ len1 + 1 ];
for( int i = 0; i < len1 + 1; ++i )
{
p[i] = new int[ len2 + 1 ];
}
for( int i = 0; i <= len1; ++i )
{
p[i][len2] = len1 - i;
}
for( int i = 0; i <= len2; ++i )
{
p[len1][i] = len2 - i;
}
p[len1][len2] = 0;
for( int i = len1 - 1; i >= 0; --i )
{
for( int j = len2 - 1; j >= 0; --j )
{
if( X[i] == Y[j] )
{
p[i][j] = p[ i + 1 ][ j + 1 ];
}
else
{
int v1 = p[ i + 1 ][j];
int v2 = p[i][ j + 1 ];
int v3 = p[ i + 1 ][ j + 1 ];
p[i][j] = minValue( v1, v2, v3 ) + 1;
}
}
}
result = p[0][0];
for( int i = 0; i < len1 + 1; ++i )
{
delete []p[i];
}
delete []p;
return result;
}
const int SIZE = 100;
int cnt1 = 0;
int dist[SIZE][SIZE];
int calStringDistance1( string X, string Y, int i, int j )
{
int result = 0;
int len1 = X.length();
int len2 = Y.length();
if( i < 0 || j < 0 || i > len1 || j > len2 )
{
cout << "calStringDistance1 func: err -1, i < 0 || j < 0 || i > len1 || j > len2" << endl;
result = -1;
return result;
}
if( i == len1 )
{
dist[i][j] = len2 - j;
result = dist[i][j];
return result;
}
if( j == len2 )
{
dist[i][j] = len1 - i;
result = dist[i][j];
return result;
}
if( dist[i][j] != -1 ) //备忘录解法
{
cnt1 = cnt1 + 1;
return dist[i][j];
}
if( X[i] == Y[j] )
{
dist[i][j] = calStringDistance1( X, Y, i + 1, j + 1 );
}
else
{
int v1 = calStringDistance1( X, Y, i + 1, j );
int v2 = calStringDistance1( X, Y, i, j + 1 );
int v3 = calStringDistance1( X, Y, i + 1, j + 1 );
dist[i][j] = minValue( v1, v2, v3 ) + 1;
}
result = dist[i][j];
return result;
}
int main()
{
int ret = 0;
for( int i = 0; i < SIZE; ++i )
{
for( int j = 0; j < SIZE; ++j )
{
dist[i][j] = -1;
}
}
//cout << calStringDistance( "ab", "c" ) << endl;
cout << calStringDistance( "awrtg234sfgtsf2r24sdgddg5sgsagadgb", "csgqsdf4tdgssafsfsafsfsgsddhdfffffffffsfsfwetxgasd" ) << endl; // 39
cout << calStringDistance1( "awrtg234sfgtsf2r24sdgddg5sgsagadgb", "csgqsdf4tdgssafsfsafsfsgsddhdfffffffffsfsfwetxgasd", 0, 0 ) << endl; // 39
cout << cnt1 << endl; // 2840
return ret;
}
/*
39
39
2840
*/
求两个字符串的编辑距离
最新推荐文章于 2024-09-11 09:00:00 发布