Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include<bits/stdc++.h> //poti scrie librariile pe care le stii aici
  2. using namespace std;
  3.  
  4. // Utility function to find minimum of three numbers
  5. int min(int x, int y, int z)
  6. {
  7. return min(min(x, y), z);
  8. }
  9.  
  10. int editDist(string str1 , string str2 , int m ,int n)
  11. {
  12. // If first string is empty, the only option is to
  13. // insert all characters of second string into first
  14. if (m == 0) return n;
  15.  
  16. // If second string is empty, the only option is to
  17. // remove all characters of first string
  18. if (n == 0) return m;
  19.  
  20. // If last characters of two strings are same, nothing
  21. // much to do. Ignore last characters and get count for
  22. // remaining strings.
  23. if (str1[m-1] == str2[n-1])
  24. return editDist(str1, str2, m-1, n-1);
  25.  
  26. // If last characters are not same, consider all three
  27. // operations on last character of first string, recursively
  28. // compute minimum cost for all three operations and take
  29. // minimum of three values.
  30. return min ( editDist(str1, str2, m, n-1)+20 , // Insert, 20 being the cost of insertion
  31. editDist(str1, str2, m-1, n)+20, // Remove, 20 being the cost of removal
  32. editDist(str1, str2, m-1, n-1)+5 // Replace, 5 being the cost of transformation
  33. );
  34. }
  35.  
  36. // Driver program
  37. int main()
  38. {
  39. // your code goes here
  40. string str1 = "sunday";
  41. string str2 = "saturday";
  42.  
  43. cout << editDist( str1 , str2 , str1.length(), str2.length());
  44.  
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement