微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C - 动态规划 - 编辑距离

如何解决C - 动态规划 - 编辑距离

(我在阅读建议后简化了我的代码

我正在编写一个程序来获取 2 个字符串并将最小编辑距离作为 int 返回。

eg. str1 = ab,str2 = ab ; //distance will be 0. 
(when both char of str1 and str2 are the same,distance will be 0)

eg. str1 = abc,str2 = c ; distance will be 2.

在我的代码中,我使用了以下字符串。

str1 = editing
str2 = distance

正确答案应该是 5(e、s、i&a、g&c、e),但我的程序返回 6。 我尝试过其他字符串,例如(短和端口),它给出了它应该是什么的答案。

我尝试过调试,但我不明白为什么有时“check”函数中的这 3 个选项(ans1、ans2、ans3)给出正确的结果,有时却没有。

 ans1 is the result when index of str1 +1
 ans2 is the result when index of str2 +1
 ans3 is the result when index of both str1 and str2 +1

我已经多次阅读我的代码,但我找不到哪里出了问题。 请帮助我了解我的问题。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int check(const char* str1,const char* str2,int i,int j,int dp[10][10])
{
  int steps = 0;
  while (str1[i] != '\0' || str2[j] != '\0')
  { //while one side has not reach '\0'
        while (str1[i] != '\0' && str2[j] != '\0')
        { //while both sides has not reach '\0'
            if (dp[i][j] != -1){ return dp[i][j];} //if there is stored answer,return that
             if (str1[i] == str2[j]) 
            { // if both char are the same,both move forward
                i++;
                j++;
            }
            else  //if both char are not the same
            { 
              int ans1 = check(str1,str2,i+1,j,dp);  //ans1 = when str1 move forward
              int ans2 = check(str1,i,j+1,dp);  //ans2 = when str2 move forward
              int ans3 = check(str1,dp);//ans3 = when both move forward

                //compare the result below to find the smallest steps(distance)
            
                if (ans1 <= ans2)
                { //ans1 is smaller than ans2
                    if (ans1 <= ans3)
                    { //ans1 is smallest
                        dp[i][j] = ans1 + 1; //store the answer to dp array
                        i++;                        //move forward
                        steps++;                    //steps +1
                    }
                    else //ans3 is smallest
                    { 
                        dp[i][j] = ans3 + 1;
                        i++;
                        j++;
                        steps++;
                    }
                }
                else //ans2 is smaller than ans1
                { 
                    if (ans2 <= ans3)
                    { //ans2 is smallest
                        dp[i][j] = ans2 + 1;
                        j++;
                        steps++;
                    }
                    else //ans3 is smallest
                    { 
                        dp[i][j] = ans3 + 1;
                        i++;
                        j++;
                        steps++;
                    }
                }
            }//else ends here
        }//while loop ends (both sides are not \0) 
        
        //when str1 or str2 reaches the end '\0'
        if (str1[i] != '\0')
        {
            i++;
            steps++;
        }
        else if (str2[j] != '\0')
        {
            j++;
            steps++;
        }
  }//while loop ends(one side is not \0)
    
  //printf("i=%d\nj=%d\nsteps=%d\n",steps);
  return steps;
  
}//function ends here


int main() {
  char str1[10] = "editing";
  char str2[10] = "distance";
    int min_step[10][10]; //with index [i][j]
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      min_step[i][j] = -1; 
  //scanf("%s %s",str1,str2);
  printf("%d\n",check(str1,min_step));
  return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。