Skip to content
TheChatArchiveThe Chat Archive
o3-mini· OpenAIHugging Face LMArena Human Preference 140K

write lcs code in c++

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Metallama-3.3-70b-instruct’s answer.
  1. User

    write lcs code in c++

  2. Assistant

    Below is an example C++ program that computes the Longest Common Subsequence (LCS) between two strings using dynamic programming:

    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    // Function to compute the LCS and return it as a string.
    string lcs(const string& X, const string& Y) {
        int m = X.size();
        int n = Y.size();
    
        // Create a DP table with (m+1) x (n+1) dimensions initialized to 0
        vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
    
        // Fill the dp table
        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] = max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
    
        // Reconstruct the LCS from the dp table
        int index = dp[m][n];
        string lcsStr(index, ' ');  // Pre-allocate space for LCS result
    
        int i = m, j = n;
        while (i > 0 && j > 0) {
            if (X[i - 1] == Y[j - 1]) {
                // If current character from both strings is same, it's part of LCS.
                lcsStr[--index] = X[i - 1];
                i--;
                j--;
            } else if (dp[i - 1][j] > dp[i][j - 1]) {
                i--;
            } else {
                j--;
            }
        }
    
        return lcsStr;
    }
    
    int main() {
        string s1, s2;
        
        cout << "Enter first string: ";
        getline(cin, s1);
        
        cout << "Enter second string: ";
        getline(cin, s2);
    
        string result = lcs(s1, s2);
        cout << "Longest Common Subsequence is: " << result << endl;
    
        return 0;
    }
    

    Explanation

    1. DP Table Initialization:
      We create a 2D vector dp of size (m+1) x (n+1) (where m and n are the lengths of X and Y respectively) and initialize it with zeros. The cell dp[i][j] will hold the length of the LCS of the substrings X[0..i-1] and Y[0..j-1].

    2. Building the Table:
      We iterate through each character of the strings. If the characters match, we set dp[i][j] = dp[i-1][j-1] + 1; otherwise, we take the maximum of the left cell (dp[i-1][j]) or the top cell (dp[i][j-1]).

    3. Reconstructing the LCS:
      Starting from dp[m][n], we trace back to determine the actual LCS string by comparing characters and moving appropriately in the table.

    4. I/O:
      The program reads two input strings from the user and outputs the computed LCS.

    You can compile this code with a C++ compiler (like g++) and run it to compute the LCS for two given strings.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.