0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-28 12:11:58 +00:00

1265. 【例9.9】最长公共子序列

http://ybt.ssoier.cn:8088/statusx.php?runidx=15567347
This commit is contained in:
Baoshuo Ren 2022-04-02 16:07:48 +08:00
parent 7e1797d2fa
commit a538709611
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

35
ybt/1265/1265.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <algorithm>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1005;
int n, m, f[N][N];
std::string a, b;
int main() {
std::ios::sync_with_stdio(false);
cin >> a >> b;
a = ' ' + a;
b = ' ' + b;
n = a.size() - 1;
m = b.size() - 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
f[i][j] = std::max({f[i][j], f[i - 1][j], f[i][j - 1]});
if (a[i] == b[j]) f[i][j] = std::max(f[i][j], f[i - 1][j - 1] + 1);
}
}
cout << f[n][m] << endl;
return 0;
}