Q: Given two strings str1 and str2, the task is to find the length of the shortest string that has both str1 and str2 as subsequences.
Eg str1 = "geek", str2 = "eke"
O/P : 5 as the string "geeke" contains both "geek" and "eke" as subsequences
int LCS(string X, string Y) {
int m=X.length();
int n=Y.length();
int dp[m+1][n+1];
// initialization
for(int i=0;i<=m;i++)
dp[i][0]=0; // Eg LCS of "abc" & "" = 0
for(int j=0;j<=n;j++)
dp[0][j]=0; // Eg LCS of "" & "abc" = 0
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(X[i-1]==Y[j-1])
dp[i][j]=1+dp[i-1][j-1];
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
return dp[m][n];
}
int SCS(string X, string Y, int m, int n)
{
return m+n-lcs(X,Y);
}