(1) double Levenshtein :: DLdis(int ms, int ns) |
(2) |
(3) double ret = 0; |
(4) int min; |
//Pretreatment |
(5) int m = ms; |
(6) int n = ns; |
//min takes the smaller between m and n |
(7) min = m < n ? m : n; |
(8) min = min = = 0 ? 1 : min; |
(9) int i, j; |
(10) double subcost, transcost; |
//Define operating costs to two |
(11) double idcost = 2; |
//Store the distance array |
(12) double dis = new double; |
//Initialize the array |
(13) for(i = 0;i < m;i++) |
(14) dis = new double; |
(15) for(i = 0; i < m;i++) |
(16) for(j = 0; j < n; j++) |
(17) dis = -1; |
//Calculate the operating costs of the first ramp line and the first vertical line |
(18) for(i = 0; i < m; i++) |
(19) dis = i idcost; |
(20) for(j = 0; j < n; j++) |
(21) dis = j idcost; |
//Calculate the operating costs of non-first rungs and first vertical lines. |
(22) for(i = 1; i < m; i++) |
(23) |
(24) for(j = 1; j < n; j++) |
(25) |
//If the two strings are equal, the operating cost is zero. |
(26) if(str1 = = str2) |
(27) subcost = transcost = 0; |
(28) else |
(29) |
//Otherwise the replacement cost is two. |
(30) subcost = 2; |
//The exchange cost is 0.1 |
(31) transcost = 0.1; |
(32) |
//The minimum cost is the edit distance, which is stored in the matrix. |
(33) dis = minimum(dis + idcost, dis + idcost, dis + subcost); |
//Two character exchanges |
(34) if(i >1 && j >1 && str1[i] = = str2[j-1] && str1[i-1] = = str2[j]) |
(35) dis[i][j] = dis[i][j] < dis[i-2][j-2] + transcost ? dis[i][j]: dis[i-2][j-2] + transcost; |
(36) |
(37) |
//Free dis |
(38) for(i = 0; i < m; i++) |
(39) delete dis[i]; |
(40) delete dis; |
(41) |