E05 线性DP 最长公共子序列
视频链接:https://www.bilibili.com/video/BV1EK411K7Eb/
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N=1010; int n, m; char a[N], b[N]; int f[N][N]; int main(){ cin>>n>>m>>a+1>>b+1; for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) if(a[i]==b[j]) f[i][j]=f[i-1][j-1]+1; else f[i][j]=max(f[i-1][j-1],max(f[i-1][j],f[i][j-1])); cout<<f[n][m]; }