POJ 1458 Common Subsequence——LCS

xiaoxiao2021-02-28  48

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 5000; char str1[maxn], str2[maxn]; int n, m, dp[maxn][maxn]; int main() { while (~scanf("%s %s", str1 + 1, str2 + 1)) { n = strlen(str1 + 1), m = strlen(str2 + 1); dp[0][0] = dp[0][1] = dp[1][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (str1[i] == str2[j]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } printf("%d\n", dp[n][m]); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-73590.html

最新回复(0)