Longest Common Subsequence in disguise. Python impl. has TLE in one case. And C++ is fine.
#include <cmath> #include <cstdio> #include <vector> #include <unordered_set> #include <string> #include <iostream> #include <algorithm> using namespace std; int main() { string a, b; cin >> a; cin >> b; size_t len = a.length(); vector<vector<int>> dp(len + 1, vector<int>(len + 1, 0)); for (int i = 0; i <= len; i++) for (int j = 0; j <= len; j++) { if (i * j == 0) dp[i][j] = 0; else if (a[i - 1] == b[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } cout << dp[len][len]; return 0; }
时间: 2024-11-08 18:26:09