传送门:https://ac.nowcoder.com/acm/contest/301/E
求最长公共子序列。
立个 flag 搞dp。
AC code:
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> #define INF 0x3f3f3f3f using namespace std; const int MAXN = 1e3+10; char str1[MAXN], str2[MAXN]; int dp[MAXN][MAXN]; int main() { scanf("%s%s", &str1, &str2); memset(dp, 0, sizeof(dp)); int len1 = strlen(str1); int len2 = strlen(str2); for(int i = 1; i <= len1; i++){ for(int j = 1; j <= len2; j++){ if(str1[i-1] == str2[j-1]) { dp[i][j] = max(dp[i-1][j-1]+1, dp[i][j]); } else{ dp[i][j] = max(dp[i-1][j], dp[i][j-1]); } } } printf("%d ", dp[len1][len2]); return 0; }