#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
int dp[1024][1024] = { 0 };
int main() {
// system("chcp 65001");
cin.tie(0);
cout.tie(0);
freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
string a, b;
while (cin >> a >> b) {
memset(dp, 0, sizeof(dp));
// dp[i][j]存i长度的a和j长度的b的最长公共子序列长度
for (int i = 1; i <= a.length(); i++) {
for (int j = 1; j <= b.length(); j++) {
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[a.length()][b.length()] << endl;
}
return 0;
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交