题目:
https://ac.nowcoder.com/acm/problem/106055
思路:
dp[i][j]表示s1前i个字母和s2前j个字母的最长子序列
#include<algorithm> #include<iostream> #include<string> #include<cstdio> using namespace std; int dp[2001][2001]; int main() { int m,n; string s1,s2; while(cin>>s1>>s2) { m=s1.length(); n=s2.length(); for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(s1[i-1]==s2[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; } }