0
我试图运行下面的代码,但它给我分割故障: -分段故障使用std ::时填写
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
int dp[MAX][MAX];
string s1, s2;
int lcs(int i, int j)
{
int val;
if (i < 0 || j < 0)
return 0;
else if (dp[i][j] != -1)
{
return dp[i][j];
}
else
{
val = max(lcs(i-1,j), lcs(i, j-1));
if (s1[i] == s2[j])
val = max(lcs(i-1,j-1) + 1, val);
}
dp[i][j] = val;
return val;
}
int main()
{
int tc;
scanf("%d", &tc);
while (tc--)
{
fill(&dp[0][0], &dp[MAX][MAX], 0);
cin>>s1;
cin>>s2;
printf("LCS = %d\n", lcs(s1.size()-1, s2.size()-1));
}
return (0);
}
现在,它给我分割故障,在printf
线在while循环中。但是,如果我注释填充语句,则不存在分段错误。
这可能是什么原因?
_“这可能是什么原因?”_未定义的行为最有可能。无法从您的代码示例中发现它。 –
为什么使用C++字符串,然后使用固定大小的C数组和宏?使用可以是所需大小的矢量,并且不会因大输入而中断。 –
也严重:[为什么我不应该#include?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)! –