mushyx @ 2023-07-20 20:20:49
#include <bits/stdc++.h>
using namespace std;
#define N 999
int df[5418][5418],a1[5418],b1[5418],n,m;
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a1[i];
for(int i=1;i<=n;i++) cin>>b1[i];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
df[i][j]=max(df[i-1][j],df[i][j-1]);
if(a1[i]==b1[j])
df[i][j]=max(df[i][j],df[i-1][j-1]+1);
}
}
cout<<df[n][m];
return 0;
}
by miaozheyuan @ 2023-08-04 18:33:35
我也改不对,只能得50分,因为n <=
#include <bits/stdc++.h>
using namespace std;
#define N 999
int df[5418][5418],a1[5418],b1[5418],n;
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a1[i];
for(int i=1;i<=n;i++) cin>>b1[i];
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
df[i][j]=max(df[i-1][j],df[i][j-1]);
if(a1[i]==b1[j])
df[i][j]=max(df[i][j],df[i-1][j-1]+1);
}
}
cout<<df[n][n];
return 0;
}
by wzxx202128 @ 2023-08-09 11:25:55
看题解,有O(nlogn)做法,用到了最长上升子序列