This is more of a "can you help clarify?" ~ the below is the full assignment I was handed, with no additional explanation.
-----------
Consider two strings as follows.
ABCBDAB
BDCABA
The “longest matched substring allowing for gaps” (LMSAFG) is the longest (could be a tie) such substring matched between the two strings such that gaps are allowed but the sequence is maintained (in either direction). So, the LMSAFG are as follows in the given example:
BCBA, BCAB, BDAB, BADB, BACB, ABCB
Write a program in C++ to print all such LMSAFG using the above test.
----------
I've done my research online, read through the geekforgeeks pages, the wikibooks for "longest common substring"
As well, we've already done an assignment for all unique substrings of a full string, so I know that's not what is to be accomplished with this assignment. What I'm assuming is a variation of the code below to print out ALL of the variations, not just one? And if so, after a few hours of trial an error, at least with this code, I'm not sure how to implement it exactly so that it would print out a list as shown in the assignment, as well as any additional not included.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
#include <algorithm>
using namespace std;
//Prints the Longest common subsequence
void printLCS( char *s1, char *s2, int m, int n )
{
int L[m+1][n+1];
//Building L[m][n] as in algorithm
for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (s1[i-1] == s2[j-1])
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
//To print LCS
int index = L[m][n];
//charcater array to store LCS
char LCS[index+1];
LCS[index] = '\0'; // Set the terminating character
//Stroing characters in LCS
//Start from the right bottom corner character
int i = m, j = n;
while (i > 0 && j > 0)
{
//if current character in s1 and s2 are same, then include this character in LCS[]
if (s1[i-1] == s2[j-1])
{
LCS[index-1] = s1[i-1]; // Put current character in result
i--; j--; index--; // reduce values of i, j and index
}
// compare values of L[i-1][j] and L[i][j-1] and go in direction of greater value.
else if (L[i-1][j] > L[i][j-1])
i--;
else
j--;
}
// Print the LCS
cout << "LCS of " << s1 << " and " << s2 << " is "<< endl << LCS << endl;
}
/* Driver program to test above function */
int main()
{
char s1[] = "ABCBDAB";
char s2[] = "BDCABA";
printLCS(s1, s2, strlen(s1), strlen(s2));
return 0;
}
|