AcWing 897. 最长公共子序列
原题链接
简单
作者:
233
,
2019-08-13 14:41:19
,
所有人可见
,
阅读 1475
C++ 代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(void) {
int leng1, leng2;
cin >> leng1 >> leng2;
string str1, str2;
cin >> str1 >> str2;
vector<vector<int>>dp(leng1 + 1, vector<int>(leng2 + 1));
for (int i = 0; i < leng1; ++i) {
for (int j = 0; j < leng2; ++j) {
if (str1[i] == str2[j])
dp[i + 1][j + 1] = dp[i][j] + 1;
else
dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
}
}
cout << dp.back().back() << endl;
return 0;
}