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 involves finding the longest sequence that appears in the same relative order in both given strings but not necessarily consecutively. For example, the LCS of "abcde" and "ace" is "ace", with a length of 3.
There are two primary approaches to solve the LCS problem in C++:
Recursive Approach: This method uses recursion to explore all possible subsequences.
Dynamic Programming Approach: This method utilizes a table to store the results of subproblems, optimizing the solution by avoiding redundant calculations.
1. Recursive Approach:
In this approach, we define a recursive function that compares characters from the end of both strings and proceeds accordingly.
Here's the C++ implementation:
#include <iostream>
#include <string>
#include <algorithm>
int lcs(const std::string& X, const std::string& Y, int m, int n) {
if (m == 0 || n == 0)
return 0;
if (X[m - 1] == Y[n - 1])
return 1 + lcs(X, Y, m - 1, n - 1);
else
return std::max(lcs(X, Y, m - 1, n), lcs(X, Y, m, n - 1));
}
int main() {
std::string X = "abcde";
std::string Y = "ace";
int m = X.size();
int n = Y.size();
std::cout << "Length of LCS is " << lcs(X, Y, m, n) << std::endl;
return 0;
}
In this code, the lcs function recursively computes the length of the LCS by comparing characters from the end of both strings. If the characters match, it adds 1 to the result and continues with the remaining substrings. If they don't match, it considers the maximum LCS by either excluding the current character of the first string or the second string.
2. Dynamic Programming Approach:
The recursive approach can be inefficient due to overlapping subproblems. Dynamic programming addresses this by storing the results of subproblems in a table, thus avoiding redundant calculations.
Here's the C++ implementation using dynamic programming:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int lcs(const std::string& X, const std::string& Y) {
int m = X.size();
int n = Y.size();
std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1, 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] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[m][n];
}
int main() {
std::string X = "abcde";
std::string Y = "ace";
std::cout << "Length of LCS is " << lcs(X, Y) << std::endl;
return 0;
}
In this implementation, a 2D vector dp is used to store the lengths of LCS for substrings of X and Y. The table is filled in a bottom-up manner, where each entry dp[i][j] represents the length of LCS of X[0..i-1] and Y[0..j-1]. The final result is found at dp[m][n].
The dynamic programming approach significantly reduces the time complexity to O(m * n), making it more efficient for larger strings.
For further reading and more detailed explanations, you can refer to the following resources:
These articles provide in-depth discussions and additional examples to help you understand and implement the LCS problem effectively.