AcWing 897. 最长公共子序列
原题链接
简单
作者:
YMYS
,
2024-12-22 11:10:28
,
所有人可见
,
阅读 10
/*
* @Author: YMYS
* @Date: 2024-02-25 21:38:22
* @LastEditTime: 2024-12-22 11:10:03
* @FilePath: \VScode-C&C++-Coding\Acwing\算法基础课\5.第五章\2.线性DP\4.最长公共子序列.cpp
* @URL:https://www.acwing.com/problem/content/899/
* @Description: 897. 最长公共子序列
*
* 题解推荐:https://www.acwing.com/solution/content/8111/
*
* 推荐的blibli视频:https://www.bilibili.com/video/BV1ey4y1d7oD/
*
* 最长公共子序列:可以断开
* 最长公共子串:不能断开
* eg:x:ABCBDAB;y:BDCABC
*
*/
#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int n,m;
char a[N],b[N];
int f[N][N];
signed main()
{
cin>>n>>m>>a+1>>b+1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
f[i][j] = max(f[i-1][j], f[i][j-1]);
if(a[i] == b[j]) f[i][j] = f[i-1][j-1] + 1;
}
}
cout << f[n][m] << endl;
return 0;
}