字符串编辑距离查表法改进

      《字符串编辑距离查表法》此篇博文所述,时空复杂度均为 O ( |s1| * |s2| )。此改进法使空间复杂度降为 0 ( min ( |s1|, |s2| ) )。

UINT StringDistance ( const _TCHAR str1[], const _TCHAR str2[] ) { UINT length1, length2, * record, i, j, cur, top; length1 = _tcslen ( str1 ); length2 = _tcslen ( str2 ); if ( length2 > length1 ) { length1 ^= ( length2 ^= ( length1 ^= length2 ) ); str1 = ( const _TCHAR * )( ( UINT ) str1 ^ ( UINT ) str2 ); str2 = ( const _TCHAR * )( ( UINT ) str2 ^ ( UINT ) str1 ); str1 = ( const _TCHAR * )( ( UINT ) str1 ^ ( UINT ) str2 ); } record = ( UINT * ) malloc ( ( length2 + 1 ) * sizeof ( UINT ) ); for ( j = 0; j <= length2; j++ ) record[j] = j; for ( i = 0; i < length1; i++ ) { top = i + 1; for ( j = 0; j < length2; j++ ) { if ( str1[i] == str2[j] ) cur = record[j]; else { cur = record[j] > record[j + 1] ? record[j + 1] : record[j]; if ( cur > top ) cur = top; cur++; } record[j] = top; top = cur; } record[j] = top; } free ( record ); return cur; // cur = top = record[j] }

你可能感兴趣的:(字符串编辑距离查表法改进)