Reputation: 3
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);
}
Upvotes: 0
Views: 1574
Reputation: 1066
2 problems:
lcs()
if
in the beginning of your lcs()
Here is corrected code:
#include<cstring>
#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) // problem 2
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(){
memset(lcs1,-1,sizeof(lcs1)); // problem 1
int temp = lcs(0,0);
printf("%d",temp);
}
Note: that it is a good practice to avoid global variables. Try to capsulate them in structures or classes. See @Matthieu Brucher's answer for proper C++ implementation.
Upvotes: 1
Reputation: 22023
Just for fun, a C++17 implementation with classes:
#include <iostream>
#include <vector>
#include <string_view>
class LCSMemoizer
{
std::vector<std::vector<int>> memory;
std::string_view s1;
std::string_view s2;
public:
LCSMemoizer(std::string_view s1, std::string_view s2)
: memory(s1.size(), std::vector<int>(s2.size(), -1)), s1(s1), s2(s2)
{
}
int run(unsigned int i1, unsigned int i2)
{
if(i1 == s1.size() || i2 == s2.size())
{
return 0;
}
if(memory[i1][i2] != -1)
{
return memory[i1][i2];
}
int sub = 0;
if(s1[i1] == s2[i2])
{
sub = run(i1+1, i2+1);
sub += 1;
}
else
{
sub = std::max(run(i1, i2+1), run(i1+1, i2));
}
memory[i1][i2] = sub;
return sub;
}
};
int lcs(std::string_view s1, std::string_view s2)
{
LCSMemoizer m(s1, s2);
return m.run(0, 0);
}
int main()
{
std::cout << lcs("bd", "abcd");
}
Also note that the usual question is not just to return the count, but also the string itself.
Upvotes: 1