Test case 0 failing and other passing


every test case except test case 0 is passing, i have used the same code which i wrote to print the lcs of two strings with minor modifications.

your code may fail on cases when there are multiple LCS for string 1 and string 2. To avoid this use 3D DP.

i have used 3d dp but i’m not getting correct answer, can you please look into it

Do these:

  1. initialize dp elements with zero;
  2. value of k goes 1 to o and not 1 to 0.

i did this, its now giving garbage value

#include<bits/stdc++.h>
#include
using namespace std;
void lcs(string s1,string s2,string s3)
{
int n=s1.length();
int m=s2.length();
int o=s3.length();
int dp[101][101][101];

for(int i=0;i<=n;i++)
{
	for(int j=0;j<=m;j++)
	{
        for(int k=0;k<=o;k++)
        {
            if(i==0 ||j==0 ||k==0)
            {
                dp[i][j][k]=0;
                continue;
            }
		if(s1[i-1]==s2[j-1] && s2[j-1]==s3[k-1]) 
		{
			dp[i][j][k]=1+dp[i-1][j-1][k-1];
		}
		else
		{
			dp[i][j][k]=max(dp[i][j][k-1],max(dp[i-1][j][k],dp[i][j-1][k]));
		}
	}
}
}
 
cout<< dp[n][m][o];

}
int main() {
string s1,s2,s3;
cin>>s1>>s2>>s3;
lcs(s1,s2,s3);

return 0;

}

there you go,
if(s1[i-1]==s2[j-1] && s2[j-1]==s3[k-1]) was wrong previously

ok ok, i got it, thank you!