Why segmentation fault is coming?

The approach you are using in your code is not correct…
Pls use the following approach as :

long long int lcs_with_k_ordered(long long int i,long long int j,long long int k)
{
if(i==n || j==m)
{
return 0;
}
if(dp[i][j][k]!=-1)
{
return dp[i][j][k];
}
long long int res=0;
if(a[i]==b[j])
{
res=1+lcs_with_k_ordered(i+1,j+1,k);
}
else
{
if(k>0)
{
res=1+lcs_with_k_ordered(i+1,j+1,k-1);
}
res=max(res,lcs_with_k_ordered(i,j+1,k));
res=max(res,lcs_with_k_ordered(i+1,j,k));

}

return dp[i][j][k]=res;
}