Minimum editing cost problem:
For two strings A and B, we need to insert, delete, and modify a string into B string, define the cost of C0,C1,C2 for three operations, design an efficient algorithm to find the minimum cost of changing a string to B string.
Given two strings a and b, and their lengths and three operating costs, return the minimum cost required to turn A string into a B-string. Ensure that both string lengths are less than or equal to 300, and that three cost values are less than or equal to 100.
Test Examples:
"ABC", 3, "ADC", 3,5,3,100
Returns: 8
Problem analysis: To see this problem, the first thought is the editing distance problem, it can be said that there are similarities, but it is necessary to pay attention to the cost of increased deletion into account.
Note: (1) The operation of adding or deleting can only be operated on a string, increase b[j]:h[i][j] = h[i][j-1]+c0; Delete A[i]:h[i][j] = H[I-1][J]+C1;
(2) Modify the operation to compare the cost of the size, because the deletion once again insert can also be considered as a modification operation.
Program implementation:
1 classMincost {2 Public:3 intMin2 (intAintb) {4 returnA<b?a:b;5 }6 intMin3 (intAintBintc) {7 returnMin2 (A, b) <c?Min2 (A, B): C;8 }9 intFindmincost (stringAintNstringBintMintC0,intC1,intC2) {Ten //Write code here One inth[n+1][m+1]; Ah[0][0] =0; - for(intI=1; i<=n;i++) -h[i][0] = i *C1; the for(intj=1; j<=m;j++) -h[0][J] = J *C0; - for(intI=1; i<=n;i++){ - for(intj=1; j<=m;j++){ + if(a[i-1] = = b[j-1]) -H[I][J] = h[i-1][j-1]; + Else{ A intDelete_cost = h[i-1][J] + C1;//Delete A[i] at intInsert_cost = h[i][j-1] + C0;//Insert B[j] - intModify_cost = h[i-1][j-1] + min2 (C0+C1,C2);//Modify A[i] to B[j] -H[I][J] =min3 (delete_cost,insert_cost,modify_cost); - } - } - } in returnH[n][m]; - } to};
Reprint please specify the source:
C + + Blog Park: godfrey_88
http://www.cnblogs.com/gaobaoru-articles/
Minimum editing cost