【发布时间】:2021-07-17 08:41:56
【问题描述】:
我正在解决这个问题:
Coding Problem
Problem Statement: Edit Distance
Problem Level: MEDIUM
Problem Description:
You are given two strings S and T of lengths M and N, respectively. Find the 'Edit Distance' between the strings.
Edit Distance of two strings is the minimum number of steps required to make one string equal to the other. In order to do so, you can perform the following three operations:
1. Delete a character
2. Replace a character with another one
3. Insert a character
Note :
Strings don't contain spaces in between.
Input format :
The first line of input contains the string S of length M.
The second line of the input contains the String T of length N.
Output format :
Print the minimum 'Edit Distance' between the strings.
Constraints :
0 <= M <= 10 ^ 3
0 <= N <= 10 ^ 3
Time Limit: 1 sec
Sample Input 1 :
abc
dc
Sample Output 1 :
2
Explanation to the Sample Input 1 :
In 2 operations we can make string T to look like string S.
First, insert character 'a' to string T, which makes it "adc".
And secondly, replace the character 'd' of string T with 'b' from the string S. This would make string T as "abc" which is also string S.
Hence, the minimum distance.
Sample Input 2 :
whgtdwhgtdg
aswcfg
Sample Output 2 :
9
我为此编写了以下代码:
#include <iostream>
using namespace std;
int EditDistance(string s, string t, int **arr)
{
int i = s.size();
int j = t.size();
//Base Case
if (t.size() == 0 || s.size()==0)
{
return max (s.size(),t.size());
}
int x, y, z, min_num;
if (arr[i][j] != (-1))
{
return arr[i][j];
}
//Recursive Case
if (s[0] == t[0])
{
arr[i][j] = EditDistance(s.substr(1), t.substr(1), arr);
return arr[i][j];
}
else
{
x = 1 + EditDistance(s.substr(1), t, arr);
y = 1 + EditDistance(s, t.substr(1), arr);
z = 1 + EditDistance(s.substr(1), t.substr(1), arr);
min_num = min(x, min(y, z));
arr[i][j] = min_num;
}
return min_num;
}
int EditDistance(string s, string t)
{
int a = s.size() + 1; //rows
int b = t.size() + 1; //columns
int **arr = new int *[b];
for (int i = 0; i < b; i++)
{
arr[i] = new int[a];
}
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{ cout << "Value of i is " << i <<endl;
cout << "Value of j is " << j <<endl;
arr[i][j] = (-1);
cout << "Value of array is " << arr[i][j] <<endl;
}
}
return EditDistance(s, t, arr);
delete[]arr;
}
int main()
{
string s1, s2;
cout << "Enter the two strings" << endl;
cin >> s1 >> s2;
cout << EditDistance(s1, s2) << endl;
return 0;
}
但是,当我尝试输入“abc”和“ab”作为字符串时,它会在函数int EditDistance(string s, string t) 的arr[i][j] = (-1) 行的i=3 和j=0 处出现分段错误。但是,我真正无法理解的是,当我创建了一个大小为 4*3 的二维数组时,为什么我会收到分段错误,然后访问元素 arr[3][0] 不应该给我分段错误。有人可以帮忙吗?
【问题讨论】:
-
不是问题,但可能是错误的 -
if (t.size() == 0 || t.size()==0)。也是很好的做法int x, y, z, min_num;,如果你不能初始化变量,你应该延迟它们的声明,直到你可以。 -
感谢您指出,但这不是问题@RichardCritten
-
@RichardCritten 谢谢你的建议,但你能回答我问的问题吗?
-
int **arr = new int *[b];更喜欢std::vector< std::vector< int > > arr;尽量不要使用 c 数组。 -
正在阅读您的代码 - 这几乎不是 minimal reproducible example - 最短的正确答案是 "user a debugger" 并自己发现这不是一项付费服务。
标签: c++ debugging segmentation-fault dynamic-programming