题解 | #Coincidence#
Coincidence
https://www.nowcoder.com/practice/f38fc44b43cf44eaa1de407430b85e69
#include <iostream> #include <cstring> using namespace std; const int N=110; int f[N][N];//f[i][j]:a[1]~a[i]和b[1]~b[j]的最长公共子序列长度 char a[N],b[N]; int main() { cin>>a+1>>b+1; for(int i=1;i<=strlen(a+1);i++){ for(int j=1;j<=strlen(b+1);j++){ f[i][j]=max(f[i-1][j],f[i][j-1]); if(a[i]==b[j]) f[i][j]=max(f[i][j],f[i-1][j-1]+1); } } cout<<f[strlen(a+1)][strlen(b+1)]<<endl; return 0; }