[b]Question->http://www.spoj.com/problems/EDIST/ .I used dynamic programming to solve the standard edit distance problem.I have checked all the corner cases.But i m getting wrong answer on spoj. Can anybody tell the counter case where this code fai
Basically we are given two strings
suppose FOOD and MONEY also mentioned in example of the question
we have to convert FOOD into MONEY and we have 3 opeartion through which we can do it.
1) Insert an element from MONEY to FOOD
2) Delete an element from FOOD
3) Replace an element from MONEY TO FOOD
but in case elements are same we will not replace it instead we will move further.
cost of each operation can be considered equal. Let it be 1:
so, Ci=Cd=Cr=1;(here i->insert,d=delete,r=replace)
let m and n be length of two strings a and b (here Food and Money resp).We take two iterators i and j
0<=i<=m & 0<=j<=n;
I used two for loops to cover each case
for insert cost(i,j)=cost(i,j-1)+Ci as insert will reduce length of string 2nd
it implies we are done with b[j]
for delete cost(i,j)=cost(i-1,j)+Cd as delete will reduce length of string ist by one it implies we are done with a[i]
for replace cost(i,j)=cost(i-1,j-1)+Cr it implies we are done with both a[i]&b[j]. but in case a[i]==b[j] we will simply do :
cost(i,j)=cost(i-1,j-1) as we are not to do any operation to make it happen.
after covering all the cases we will choose the path which cost the minimum
i consider the 2d matrix for this and cost(m)(n) give me the leat cost