LCS
import java. util. Arrays;
import java. util. Scanner;
public class 最长公共子序列 {
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
int n = sc. nextInt ( ) ;
int m = sc. nextInt ( ) ;
sc. nextLine ( ) ;
String s1 = sc. nextLine ( ) ;
String s2 = sc. nextLine ( ) ;
int [ ] [ ] f = new int [ 1010 ] [ 1010 ] ;
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< m; j++ ) {
f[ i] [ j] = Math. max ( i> 0 ? f[ i- 1 ] [ j] : 0 , j> 0 ? f[ i] [ j- 1 ] : 0 ) ;
if ( s1. charAt ( i) == s2. charAt ( j) ) f[ i] [ j] = Math. max ( f[ i] [ j] , ( i> 0 && j> 0 ) ? ( f[ i- 1 ] [ j- 1 ] + 1 ) : 1 ) ;
}
}
System. out. println ( f[ n- 1 ] [ m- 1 ] ) ;
}
}
LIS
import java. util. Scanner;
public class 最长上升子序列 {
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
int n = sc. nextInt ( ) ;
int [ ] arr = new int [ n+ 1 ] ;
int [ ] f = new int [ n+ 1 ] ;
for ( int i= 1 ; i<= n; i++ ) arr[ i] = sc. nextInt ( ) ;
int ans = - 1 ;
for ( int i= 1 ; i<= n; i++ ) {
f[ i] = 1 ;
for ( int j= 1 ; j< i; j++ ) {
if ( arr[ j] < arr[ i] ) f[ i] = Math. max ( f[ i] , f[ j] + 1 ) ;
}
ans = Math. max ( ans, f[ i] ) ;
}
System. out. println ( ans) ;
}
}