题目链接:最长公共子序列
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner;public class Main {public static void main(String[] args){Scanner scanner=new Scanner(System.in);int n=scanner.nextInt();int m=scanner.nextInt();int[] a=new int[n+1];int[] b=new int[m+1];for(int i=1;i<=n;i++){a[i]=scanner.nextInt();}for(int i=1;i<=m;i++){b[i]=scanner.nextInt();}int[][] dp=new int[n+1][m+1];for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(a[i]==b[j]){dp[i][j]=dp[i-1][j-1]+1;}else{dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);}}}System.out.println(dp[n][m]);} }