Ss1132313926 @ 2024-03-05 10:45:57
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int n=scanner.nextInt();
int []arr1=new int[n];
int []arr2=new int[n];
for(int i=0;i<n;i++){
arr1[i]=scanner.nextInt();
}
for(int i=0;i<n;i++){
arr2[i]=scanner.nextInt();
}
int [][]dp=new int[n][n];
//初始化
dp[0][0]=arr1[0]==arr2[0]?1:0;
for(int i=1;i<n;i++){
if(arr1[i]==arr2[0]){
dp[i][0]=1;
}else {
dp[i][0]=dp[i-1][0];
}
}
for(int j=1;j<n;j++){
if(arr2[j]==arr1[0]){
dp[0][j]=1;
}else {
dp[0][j]=dp[0][j-1];
}
}
//递推
for(int i=1;i<n;i++){
for(int j=1;j<n;j++){
int p1 =dp[i][j-1];
int p2 =dp[i-1][j];
int p3=arr1[i]==arr2[j]?(1+ dp[i-1][j-1]):0;
dp[i][j]=Math.max(p1,Math.max(p3,p2));
}
}
System.out.println(dp[n-1][n-1]);
}
}
by Ss1132313926 @ 2024-03-05 10:46:44
没过的都是mle,不知道怎么优化好了
by 2Bxzj @ 2024-03-19 14:29:38
虽然我学C++,但是我看了你的思路,题目给的空间不够开
而且时间复杂度是