DYNAMIC PROGRAMMING PROBLEM.I am not able to find the problem with this code.please help me to fix

//longest common subsequence
//dp BOTTOM UP
#include<bits/stdc++.h>
#include
using namespace std;

int lcs(char x[],char y[])
{
int m=strlen(x);
int n=strlen(y);
int dp[100][100];

//base case
for(int i=0;i<=m;i++)
dp[i][0]=0;
for(int j=0;j<=n;j++)
dp[0][j]=0;

for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
int q=0;
if(x[i-1]==y[j-1])
{
q=1+dp[i-1][j-1];

    }
    else
    {
    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
    }
    dp[i][j]=q;
}

}

return dp[m][n];
}

int main()
{
char str1[100],str2[100];
cin>>str1;
cin>>str2;

int ans=lcs(str1,str2);
cout<<ans;
return 0;

}

Hey @hrit04
Instead of
else
{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
You need to write
else
{
q=max(dp[i-1][j],dp[i][j-1]);
}

coz at theend you’rewriting dp[I][j] = q;

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.