wrx20100919 @ 2024-05-06 17:36:28
#include<bits/stdc++.h>
using namespace std;
int n,a[1005],b[1005],dp[1005][1005];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
cin>>b[i];
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
{
if(a[i]==b[j]){
dp[i][j]=dp[i-1][j-1]+1;
}
else{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
cout<<dp[n][n];
return 0;
}
by ___Furina___ @ 2024-05-06 17:55:53
@wrx20100919 所以你完全不看
by _C_ccx_N_ @ 2024-05-06 18:04:19
@wrx20100919 时间复杂度
by _C_ccx_N_ @ 2024-05-06 18:04:46
@wrx20100919 数据范围
by Tomle @ 2024-05-06 19:02:30
你这 dp 也有问题
if(a[i]==b[j]){
dp[i][j]=dp[i-1][j-1]+1;
}
else{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
改为
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
if(a[i]==b[j]){
dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
}
by _Clown__ @ 2024-05-07 19:22:33
@wrx20100919 标号