I am trying to run the following code, but it is giving me segmentation fault :-
#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);
}
Now, it is giving me a segmentation fault, at the printf
line in while loop. However, if I comment out the fill statement, then there is no segmentation error.
What could be a possible reason for this ?