import java.util.Scanner; public class LCS0_ver2 { public static int solveLCS(String x, String y) { int sol1, sol2, sol3; if (x.length() == 0 || y.length() == 0) { return(0); } /* System.out.println("x = " + x + ", y = " + y); System.out.println("x.lastchar = " + x.charAt(x.length()-1) + ", y.lastchar = " + y.charAt(y.length()-1) ); */ if ( x.charAt(x.length()-1) == y.charAt(y.length()-1) ) { sol1 = solveLCS(x.substring(0,x.length()-1), y.substring(0,y.length()-1) ); return(sol1 + 1); } else { sol2 = solveLCS(x.substring(0,x.length()-1), y); sol3 = solveLCS(x, y.substring(0,y.length()-1)); if ( sol2 >= sol3 ) return( sol2 ); else return( sol3 ); } } public static void main(String[] args) { String x; String y; int r; Scanner in = new Scanner(System.in); System.out.print("String x = "); x = in.next(); System.out.print("String y = "); y = in.next(); r = solveLCS(x, y); System.out.println("LCS(x,y) = " + r); } }