in c++
- User
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.
in c++
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, here's the C++ code for finding the Longest Common Subsequence (LCS) of two strings using dynamic programming.
This code provides two main functionalities:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // For std::max and std::reverse
// Function to calculate the length of the LCS of two strings
int lcsLength(const std::string& str1, const std::string& str2) {
int m = str1.length();
int n = str2.length();
// Create a DP table (2D vector) to store lengths of LCSs.
// dp[i][j] will store the length of the LCS of str1[0...i-1] and str2[0...j-1]
// Add +1 to dimensions for base cases (empty strings)
std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1, 0));
// Build the dp table in bottom-up fashion
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
// If the current characters match
if (str1[i - 1] == str2[j - 1]) {
// Add 1 to the LCS length of the strings excluding these characters
dp[i][j] = 1 + dp[i - 1][j - 1];
} else {
// If characters don't match, take the maximum LCS length
// by either excluding the current char from str1 or from str2
dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
// The value at dp[m][n] contains the length of LCS for the entire strings
return dp[m][n];
}
// Function to find and return the actual LCS string
std::string findLCS(const std::string& str1, const std::string& str2) {
int m = str1.length();
int n = str2.length();
// --- DP Table Calculation (same as in lcsLength) ---
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 (str1[i - 1] == str2[j - 1]) {
dp[i][j] = 1 + dp[i - 1][j - 1];
} else {
dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
// --- End DP Table Calculation ---
// --- Backtrack from dp[m][n] to construct the LCS string ---
std::string lcs_str = "";
int i = m;
int j = n;
while (i > 0 && j > 0) {
// If current characters in str1 and str2 are same, then
// current character is part of LCS
if (str1[i - 1] == str2[j - 1]) {
lcs_str += str1[i - 1]; // Append the character
i--; // Move diagonally up-left
j--;
}
// If characters are not same, find the larger of two paths
// and go in the direction of the larger value
else if (dp[i - 1][j] > dp[i][j - 1]) {
i--; // Move up
} else {
j--; // Move left
}
}
// The LCS string is built in reverse order, so reverse it
std::reverse(lcs_str.begin(), lcs_str.end());
return lcs_str;
}
int main() {
std::string str1 = "AGGTAB";
std::string str2 = "GXTXAYB";
std::cout << "String 1: " << str1 << std::endl;
std::cout << "String 2: " << str2 << std::endl;
int length = lcsLength(str1, str2);
std::cout << "Length of LCS: " << length << std::endl; // Output: 4
std::string lcs = findLCS(str1, str2);
std::cout << "LCS: " << lcs << std::endl; // Output: GTAB (or potentially others if multiple LCS exist)
std::cout << "\n---\n" << std::endl;
std::string str3 = "ABCBDAB";
std::string str4 = "BDCAB";
std::cout << "String 3: " << str3 << std::endl;
std::cout << "String 4: " << str4 << std::endl;
length = lcsLength(str3, str4);
std::cout << "Length of LCS: " << length << std::endl; // Output: 4
lcs = findLCS(str3, str4);
std::cout << "LCS: " << lcs << std::endl; // Output: BDAB (or BCAB, BCB etc.)
return 0;
}
Explanation:
iostream), strings (string), dynamic arrays (vector), and standard algorithms (algorithm) like max and reverse.lcsLength Function:
const std::string&) as input for efficiency (avoids copying).m and n.dp of size (m+1) x (n+1) and initializes all elements to 0. dp[i][j] will store the LCS length of the first i characters of str1 and the first j characters of str2. The extra row and column handle the base cases where one of the strings is empty.dp table, corresponding to index 0 in the strings).str1[i-1] == str2[j-1], the LCS length increases by 1 compared to the LCS of str1[0...i-2] and str2[0...j-2]. So, dp[i][j] = 1 + dp[i-1][j-1].str1 (dp[i-1][j]) or excluding the current character of str2 (dp[i][j-1]).dp[m][n] holds the length of the LCS for the full strings, which is returned.findLCS Function:
dp table as lcsLength. (Alternatively, you could pass the computed dp table to this function if you wanted to avoid recalculation).dp table, starting from dp[m][n].lcs_str.i = m and j = n.str1[i-1] == str2[j-1], this character is part of the LCS. Append it to lcs_str and move diagonally up-left in the dp table (i--, ).Time and Space Complexity:
m and n are the lengths of the two strings. This is due to filling the m x n DP table.j--dp[i][j]:
dp[i-1][j] > dp[i][j-1], move up (i--).dp[i][j-1] >= dp[i-1][j]), move left (j--).i or j reaches 0.std::reverse is used to get the correct order.lcs_str is returned.main Function: