这题的话,边界处理起来比较复杂,我归纳了讨论,最后还是wa了,哎,看了dicuss的代码,处理还是巧妙
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <string> #include <cstring> using namespace std; const int MAXN = 1E3+10; int dp[MAXN][MAXN]; int main() { string s1,s2; while(cin>>s1>>s2){ memset(dp,0,sizeof(dp)); int n1 = s1.length(); int n2 = s2.length(); for(int i=1;i<=n1;i++){ for(int j=1;j<=n2;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[n1][n2]<<endl; } return 0; }
时间: 2024-10-18 17:38:09