fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int minDistance(string word1, string word2) {
  6. auto min3=[](int a,int b,int c)
  7. {
  8. return min(a,min(b,c));
  9. };
  10. int m=word1.size();
  11. int n=word2.size();
  12. int dp[m+1][n+1];
  13. memset(dp,0,sizeof(dp));
  14. for(int i=1;i<=m;++i)
  15. {
  16. dp[i][0]=i;
  17. }
  18. for(int i=1;i<=n;++i)
  19. {
  20. dp[0][i]=i;
  21. }
  22. for(int i=1;i<=m;++i)
  23. {
  24. for(int j=1;j<=n;++j)
  25. {
  26. if(word1[i-1]==word2[j-1])
  27. {
  28. dp[i][j]=dp[i-1][j-1];
  29. }
  30. else
  31. {
  32. dp[i][j]=1+min3(dp[i-1][j-1],dp[i-1][j],dp[i][j-1]);
  33. }
  34. }
  35. }
  36. return dp[m][n];
  37. }
  38.  
  39. int main() {
  40.  
  41. string word1 = "ioana";
  42. string word2 = "dana";
  43.  
  44. cout<<minDistance(word1, word2);
  45. }
Success #stdin #stdout 0.01s 5476KB
stdin
Standard input is empty
stdout
2