/* An instance of this class is for the purpose of providing information ** about longest common subsequences of (any prefixes of) two particular ** strings. In creating an instance, the client specifies the pair of ** strings of interest, x and y. Let M and N be the lengths of x and y, ** respectively. The object is able to do the following: ** ** (1) Print a table showing, for each m<=M and n<=N, the length of any ** longest common subsequence (LCS) of x[0..m) and y[0..n) (i.e., ** the prefixes of x and y of lengths m and n, respectively). ** (2) Answer the question: For given m<=M and n<=N, what is the length ** of an LCS of x[0..m) and y[0..n)? Of course, the answer to any such ** question is simply one table entry from (1).) ** (3) Determine, for any m<=M and n<=N, one LCS of x[0..m) and y[0..n). ** (4) Determine, for any m<=M and n<=N, one matching of x[0..m) and y[0..n) ** that gives rise to an LCS of those two strings. ** ** Author: R. McCloskey, 2015 (modified in April 2019 for purposes of ** SE 504 programming assignment) */ public class LongComSubseq_504_S19 { // instance variables // ------------------ private String x, y; // the Strings of interest private int M, N; // their lengths (redundant data) private int[][] llcs; // 2-dimensional array storing values of the // LLCS function applied to x and y. // constructor (and supporing method) // ----------- /* Establishes the two given Strings as those that are the focus of ** this object. */ public LongComSubseq_504_S19(String s1, String s2) { x = s1; y = s2; computeLLCS(); // Fill the llcs[][] array with LLCS function values } /* Constructs and fills llcs[][] so that, for all i in 0..M and j in 0..N ** (where M and N are the lengths of x and y), llcs[i][j] = LLCS.i.j, ** which is to say that, upon completion of the method's execution, the ** value of llcs[i][j] is the length of a longest common subsequence of ** x[0..i) and y[0..j). */ private void computeLLCS() { M = x.length(); N = y.length(); llcs = new int[M+1][N+1]; // fill row zero and column zero with zeros for (int j=0; j != N+1; j++) { llcs[0][j] = 0; } for (int i=0; i != M+1; i++) { llcs[i][0] = 0; } int m=1, n=1; // loop invariant: (A i,j | 0<=i