troubleshooting Question

Longest common subsequence

Avatar of slritmmi
slritmmi asked on
Algorithms
9 Comments1 Solution1149 ViewsLast Modified:
I have a programming assignment that I have to implement the algorithm for solving the Longest Common Subsequence problem.
the program has to be
•prompts the user to enter a pair of strings
•displays the LCS table produced by the algorithm
•displays the longest common subsequence found
•allows the user to repeat the process with a new pair of strings

This is what I have so far... It compilied but get stoped after I enter two strings...
Could you help me out please?


import java.util.*;

public class LCS 
{
    static Scanner console = new Scanner(System.in);
    
    public static void main(String[] args) 
    {
        int i,j;
        String X;  /* String X */
        String Y;    /* String Y */
        System.out.println("Enter the pair of strings");
        X = console.next();
        Y = console.next();
        System.out.println();
        
        int m = X.length();
        int n = Y.length();
        int[][] T = new int[m+1][n+1];
      
        // C[i][0] = 0 for 0 to m 
        for (i = 0; i <= m; i++) 
        {
            T[i][0] = 0;
        }
	
        // C[0][j] = 0 for  j=0 to n 
        for (j = 0; j <= n; j++) 
        {
            T[0][j] = 0;
        }
        
        //FOR i = 1 TO m
        for (i = 1; i <= m; i++) 
        {
            for (j = 1; j <= n; j++) // FOR i = 1 TO n 
            {
                if (X.charAt(i-1) == Y.charAt(j-1)) 
                    T[i][j]=T[i-1][j-1]+1;
                    else 
                    {	
                    	    T[i][j]=java.lang.Math.max(T[i][j-1],T[i-1][j]);
                    	    
                    }
                }
        }
     	  
        // Backtracking 
        String lcs = new String();
        i=m;
        j=n;
        while (i!=0 && j!=0) 
        {
            if (T[i][j] ==1) 
            {    // diagonal 
            	lcs =X.charAt(i-1) + lcs;
                i = i - 1;
                j = j - 1;
            }
            if (T[i][j] == 2) 
            {  // up 
                i = i - 1;
            }
            if (T[i][j] == 3) 
            {  // backword 
                j = j - 1;
            }
        }
     
        /* print out the result */
        System.out.println("String X is " + X);
        System.out.println("String Y is " + Y);
        System.out.println("The length of LCS is " + T[n][m]);
        System.out.println("The LCS is " + lcs);
        System.out.println(T[i][j]);
         
    }
         
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros