C 利用編輯距離演算法查重(非構建二維陣列)

2021-10-04 20:44:08 字數 1093 閱讀 8616

/// 

/// 文字相似度(編輯距離演算法,精度最高,花費時間最多)

///

///

///

/// 比對進度,百分比數值

/// 插入乙個字元的權重

/// 替換乙個字元的權重

/// 刪除乙個字元的權重

/// 文字相似度

public

static

decimal

getlevenshteinsimilarity

(string s1,

string s2, action<

int> pro =

null

,int cost_ins =1,

int cost_rep =1,

int cost_del =1)

if(l2 ==0)

var p1 = enumerable.

range(0

, s2.length +1)

.select

(x =

> x * cost_ins)

.toarray()

;var p2 = enumerable.

repeat(0

, s2.length +1)

.toarray()

;int c0 =

0, c1 =

0, c2 =0;

for(

var i1 =

0; i1 < l1; i1++

) c2 = p2[i2]

+ cost_ins;

if(c2 < c0)

p2[i2 +1]

= c0;

}var tmp = p1;

p1 = p2;

p2 = tmp;

pro?

.invoke

(i1 *

100/ l1);}

c0 = p1[l2]

;return1-

(decimal

)c0 / math.

max(l1, l2)

;}

編輯距離及編輯距離演算法

編輯距離概念描述 編輯距離,又稱levenshtein距離,是指兩個字串之間,由乙個轉成另乙個所需的最少編輯操作次數。許可的編輯操作包括將乙個字元替換成另乙個字元,插入乙個字元,刪除乙個字元。例如將kitten一字轉成sitting sitten k s sittin e i sitting g 俄...

編輯距離及編輯距離演算法

include include include using namespace std const int max 1001 int maxlen max max int maxlen string str1,string str2 return maxlen len1 len2 int main ...

編輯距離及編輯距離演算法

include include include using namespace std const int max 1001 int maxlen max max int maxlen string str1,string str2 return maxlen len1 len2 int main ...