編輯距離演算法 java

2021-07-22 08:52:51 字數 925 閱讀 8702

/** * 編輯距離(刪除,新增,替換 得到相等字串所需次數)演算法

* s = "eeba", t="abac"

* 使用乙個二維陣列記錄所需編輯次數(s為縱向,t為橫向),

0 1 2 3 4

1 1 2 3 4

2 2 2 3 4

3 3 2 3 4

4 3 3 2 3

第二列為當t取乙個字元a的時候,s依次為 ""、"e"、"ee"、"eeb"、"eeba"所需的編輯距離

其餘的類似

以動態規劃角度來看,以edit(i,j)來代表矩陣中的元素,其意義為i代表s取前i個字元,j代表t取前個字元

如edit(2,2)代表s="ee",t="ab"的編輯距離,在矩陣中為2(**中還有乙個0行)

edit(i,j)=minist(edit(i,j-1)+1, edit(i-1,j)+1, edit(i-1,j-1)+cost)

edit(2,2)=minist(edit(2,1)+1, edit(1,2)+1, edit(1,1)+cost)

即取("ee","a")("e","ab")("ee","ab")三個編輯距離中的最小值

s.charat(i-1)==t.charat(j-1)時,cost=1

* @author yanjie

* */

public class editdistance

//返回三者最小值

private static int minimum(int a, int b, int c) {

int im = a參考

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

編輯距離概念描述 編輯距離,又稱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 ...