sotanishy's competitive programming library

sotanishy's code snippets for competitive programming

View the Project on GitHub sotanishy/cp-library-cpp

:warning: Longest Common Subsequence
(dp/lcs.hpp)

Code

#pragma once
#include <algorithm>
#include <string>
#include <vector>

/**
 * @brief Longest Common Subsequence
 */
int longest_common_subsequence(const std::string& s, const std::string& t) {
    const int n = s.size(), m = t.size();
    std::vector dp(n + 1, std::vector<int>(m + 1));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (s[i] == t[j]) {
                dp[i + 1][j + 1] = dp[i][j] + 1;
            } else {
                dp[i + 1][j + 1] = std::max(dp[i][j + 1], dp[i + 1][j]);
            }
        }
    }
    return dp[n][m];
}
#line 2 "dp/lcs.hpp"
#include <algorithm>
#include <string>
#include <vector>

/**
 * @brief Longest Common Subsequence
 */
int longest_common_subsequence(const std::string& s, const std::string& t) {
    const int n = s.size(), m = t.size();
    std::vector dp(n + 1, std::vector<int>(m + 1));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (s[i] == t[j]) {
                dp[i + 1][j + 1] = dp[i][j] + 1;
            } else {
                dp[i + 1][j + 1] = std::max(dp[i][j + 1], dp[i + 1][j]);
            }
        }
    }
    return dp[n][m];
}
Back to top page