Certainly! Below is a complete C++ program that implements the Longest Common Subsequence (LCS) algorithm. This program includes both the calculation of the length of the LCS and the reconstruction of the LCS itself.
C++ Code for Longest Common Subsequence (LCS)
#include <iostream>
#include <vector>
#include <string>
// Function to find the length of the LCS
int lcsLength(const std::string& str1, const std::string& str2) {
int m = str1.length();
int n = str2.length();
// Create a 2D array to store the lengths of common subsequences
std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1, 0));
// Fill the 2D array in a bottom-up manner
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (str1[i - 1] == str2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
// The length of the LCS is stored in the bottom-right corner of the 2D array
return dp[m][n];
}
// Function to print the LCS
void printLCS(const std::string& str1, const std::string& str2) {
int m = str1.length();
int n = str2.length();
// Create a 2D array to store the lengths of common subsequences
std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1, 0));
// Fill the 2D array in a bottom-up manner
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (str1[i - 1] == str2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
// Print the LCS in a top-down manner
int i = m;
int j = n;
std::string lcs;
while (i > 0 && j > 0) {
if (str1[i - 1] == str2[j - 1]) {
lcs = str1[i - 1] + lcs; // Prepend the character to the LCS
i--;
j--;
} else if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
} else {
j--;
}
}
std::cout << "LCS: " << lcs << std::endl;
}
int main() {
std::string str1 = "AGGTAB";
std::string str2 = "GXTXAYB";
int length = lcsLength(str1, str2);
std::cout << "Length of LCS: " << length << std::endl;
printLCS(str1, str2);
return 0;
}
Explanation of the Code
-
Function lcsLength:
- This function calculates the length of the longest common subsequence between two strings using dynamic programming.
- It creates a 2D vector
dp where dp[i][j] holds the length of the LCS of the first i characters of str1 and the first j characters of str2.
- The function iterates through both strings and fills the
dp table based on whether the characters match or not.
-
Function printLCS:
- This function reconstructs the longest common subsequence from the
dp table.
- It starts from the bottom-right corner of the
dp table and traces back to find the characters that are part of the LCS.
-
main Function:
- This is the entry point of the program where two example strings are defined.
- It calls
lcsLength to get the length of the LCS and printLCS to display the actual LCS.
Example Output
When you run the program, it will output:
Length of LCS: 4
LCS: GTAB
Compilation and Execution
To compile and run this code, you can use any C++ compiler. For example, if you are using g++, you can compile it with:
g++ -o lcs lcs.cpp
And then run it with:
./lcs
This will execute the program and display the length of the LCS and the LCS itself.