Longest common subsequence debug code


i am not able to pass one testcase…

hey @Mrinali_Mangal, i>0 and j>0 in while()

hi @Gaurav13998 after correction,still one case failed.

hey @Mrinali_Mangal,your dp table is absolutely fine, your printing logic is not correct.Your code is not giving right answer for long answer because printing logic is not correct (reversing is causing error).

Consider this logic for printing
int index=dp[m][n];
char common[index+1];
common[index]=’\0’;
int temp = index;
int i=m;
int j=n;
while(i>0 && j>0)
{
if(ch1[i-1]==ch2[j-1])
{
common[index-1]=ch1[i-1];
i–;
j–;
index–;
}
else if(dp[i-1][j]>dp[i][j-1])
{
i–;
}
else
{
j–;
}
}
for(int k=0; k<temp; k++){
cout<<common[k];
}
cout<<endl;