This program is of longest common subsequence using memoization. But it is giving answer 0 for the below example. Before adding memoization, it was giving correct answer 2.
I think I did mistake in adding memoization. Can anyone help me with what is wrong with this code?
#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
char a[100]="bd",b[100]="abcd";
int lcs1[100][100];
int lcs(int i,int j){
int temp;
if(lcs1[i][j]!=-1)
if(a[i]=='\0'||b[i]=='\0'){
return 0;
}
if(lcs1[i][j]!=-1)
return lcs1[i][j];
else if (a[i]==b[j])
temp = 1+lcs(i+1,j+1);
else
temp = max(lcs(i+1,j),lcs(i,j+1));
lcs1[i][j] = temp;
return temp;
}
int main(){
int temp = lcs(0,0);
memset(lcs1,-1,sizeof(lcs1));
printf("%d",temp);
}