write lcs code in c++
- User
write lcs code in c++
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
write lcs code in c++
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The Longest Common Subsequence (LCS) problem is a classic problem in computer science and operations research that involves finding the longest sequence that appears in the same order in two or more sequences.
#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;
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;
}
The code consists of two functions: lcsLength and printLCS. The lcsLength function calculates the length of the LCS using dynamic programming, while the printLCS function prints the actual LCS.
The lcsLength function creates a 2D array dp to store the lengths of common subsequences. It fills the array in a bottom-up manner, where dp[i][j] represents the length of the LCS between the first i characters of str1 and the first j characters of str2.
The printLCS function also creates a 2D array dp to store the lengths of common subsequences. However, it fills the array in a bottom-up manner and then prints the LCS in a top-down manner.
In the main function, we call lcsLength and printLCS with two example strings str1 and str2. The output shows the length of the LCS and the actual LCS.
The time complexity of the lcsLength and printLCS functions is O(m * n), where m and n are the lengths of the input strings. This is because we fill the 2D array dp in a bottom-up manner, which requires iterating over each character in both strings.
The space complexity of the lcsLength and printLCS functions is O(m * n), where m and n are the lengths of the input strings. This is because we create a 2D array dp to store the lengths of common subsequences, which requires O(m * n) space.