题解 | #Coincidence#
Coincidence
https://www.nowcoder.com/practice/f38fc44b43cf44eaa1de407430b85e69
// 最长公共子序列
#include<bits/stdc++.h>
using namespace std;
const int maxn=101;
string str1,str2;
int main(){
while (cin>>str1>>str2)
{
int m=str1.size();int n=str2.size();
int dp[maxn][maxn];
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if(str1[i-1]==str2[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
cout<<dp[m][n]<<endl;
}
}

查看2道真题和解析