Question: Overview The longest common substring algorithm is presented elsewhere in this book. The longest common subsequence algorithm is similar. Unlike a substring, a subsequence need
Overview
The longest common substring algorithm is presented elsewhere in this book. The longest common subsequence algorithm is similar. Unlike a substring, a subsequence need not be continuous. Ex: "ARTS" is a subsequence of ALGORITHMS, but is not a substring.
A dynamic programming matrix can be used to solve the longest common subsequence LCS problem. Rules for populating the matrix differ slightly from the longest common substring algorithm. Both algorithms populate rows from top to bottom, and left to right across a row. Each entry matrixRC is computed as follows:
If characters match, both algorithms assign matrixRC with matrixR C
If characters do not match, the longest common
substring algorithm assigns matrixRC with subsequence algorithm assigns matrixRC with maxmatrixR C matrixRC
Each algorithm uses for out of bounds entries. Ex: When computing matrix for a character match, is used instead of trying to access matrix
Sample matrix
The image below shows the longest common subsequence matrix for strings "ALASKAN" and "BANANAS". Entries corresponding to a character match are highlighted.
The largest number in the matrix indicates the length of the longest common subsequence. Ex: The largest entry in the matrix above is so the longest common subsequence between "ALASKAN" and "BANANAS" is characters long.
Multiple longest common subsequences
A pair of strings may have more than one longest common subsequence. Ex: "ALASKAN" and "BANANAS" have three longest common subsequences: "AAA", "AAN", and "AAS".
Step : Determine how to make the LCS set from the matrix
The matrix can be used to build a set of all longest common subsequences. Before writing any code, consider two options for building the LCS set:
Build the entire numerical matrix, then traverse the matrix to build the LCS set.
Build a matrix of structures that include the numerical entry plus the LCS set.
The figure below illustrates approach #
Step : Implement the LCSMatrix class
The LCSMatrix class is declared in the LCSMatrix.h file. The constructor, GetEntry and GetLongestCommonSubsequences member functions must be completed. A member variable must also be added for the matrix data. Each matrix entry may be an integer or a more complex object, depending on the choice made in step
After adding a member variable for the matrix, complete the member functions to satisfy the requirements below.
Constructor: Two lines of code are given to assign the rowCount and columnCount variables with strings length and strings length, respectively. The remainder of the function must be implemented to build the longest common subsequence matrix.
Use casesensitive character comparisons. Ex: a and A are not equal.
GetEntry: Returns the numerical entry at the given row and column indices, or if either index is out of bounds.
GetLongestCommonSubsequences: Returns an unorderedset of strings indicating all longest common subsequences for the two strings passed to the constructor.
Step : Test code, then submit
Code in main.cpp runs several test cases. Each tests the set returned from GetLongestCommonSubsequences Some test cases also check matrix entries returned from GetEntry Ensure that all tests in main.cpp pass before submitting code for grading.
#ifndef LCSMATRIXH
#define LCSMATRIXH
#include
#include
TODO: Type your code here
Include any required additional headers
Declare any desired classesstructs to be used by LCSMatrix
class LCSMatrix
private:
int rowCount;
int columnCount;
TODO: Type your code here
public:
LCSMatrixstd::string& str std::string& str
thisrowCount int strlength;
thiscolumnCount int strlength;
TODO: Type your code here
Your code here, if needed
If matrix data is dynamically allocated, add destructor
Returns the number of columns in the matrix, which also equals the length
of the second string passed to the constructor.
int GetColumnCount
return columnCount;
Returns the matrix entry at the specified row and column indices, or if
either index is out of bounds.
int GetEntryint rowIndex, int columnIndex
TODO: Type your code here remove placeholder line below
return ;
Returns the number of rows in the matrix, which also equals the length
of the first string passed to the constructor.
int GetRowCount
return rowCount;
Returns the set of distinct, longest common subsequences between the two
strings that were passed to the constructor.
std::unorderedset GetLongestCommonSubsequences
TODO: Type your code here remove placeholder line below
return std::unorderedset;
;
#endif
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
