public static int MaxLengthSubSequence(String str1, String str2){ int max = 0; int n1 = str1.length(); int n2 = str2.length(); if(str1!=null && str2!=null && n1>0 && n2>0){ int m[][] = new int[n1+1][n2+1]; for(int i=1;i<=n1;i++){ for(int j=1;j<=n2;j++){ if(str1.charAt(i-1) == str2.charAt(j-1)){ m[i][j] = m[i-1][j-1]+1; }else{ m[i][j] = Math.max(m[i-1][j], m[i][j-1]); } if(max < m[i][j]){ max = m[i][j]; } } } for(int i=0;i<m.length;i++){ for(int j=0;j<m[i].length;j++){ System.out.print(m[i][j]+" "); } System.out.println(); } } return max; }
原文:https://www.cnblogs.com/duanjiapingjy/p/9688358.html