Question

In: Computer Science

develop a recursive function to compute LCS (x,y)

develop a recursive function to compute LCS (x,y)

Solutions

Expert Solution

public static String lcs(String X, String Y)
{
    int a = X.length();
    int b = Y.length();
    if(a == 0 || b == 0){
        return "";
 }else if(X.charAt(a-1) == Y.charAt(b-1)){
        return lcs(X.substring(0,a-1),Y.substring(0,b-1))
            + X.charAt(a-1);
    }else{
        String x = lcs(X, Y.substring(0,b-1));
        String y = lcs(X.substring(0,a-1), b);
        return (x.length() > y.length()) ? x : y;
    }
}

Explanation:LCS is longest common subsequence between the two strings.For eg in "XYZ" and "YZS" lcs is YZ

In this program firstly we stored the length of strings in a and b respectively.After that if size of string is 0 then obviously we print nothing thenwe checked if character at a-1 length of X is equal to character at b-1 lengthof Y then we call LCS function with substring 0 to a-1 and o to b-1 respectively otherwise we take two more strings x and y and store the LCS call with X,Y.substring(0,b-1) and X.substring(0,a-1),b and that is all.Thankyou


Related Solutions

Develop a recursive algorithm that multiply two integer numbers x and y, where x is an...
Develop a recursive algorithm that multiply two integer numbers x and y, where x is an m-bit number and y is an n-bit number (10 points), and analyze the time complexity of this algorithm (10 points).
The following equation can be used to compute values of y as a function of x:...
The following equation can be used to compute values of y as a function of x: ? = ?? −??sin(??)(0.012? 4 − 0.15? 3 + 0.075? 2 + 2.5?) where a and b are parameters. Write a Matlab script file (m file) with the following steps: Step 1: Define scalars a = 2, b = 5. Step 2: Define vector x holding values from 0 to π/2 in increments of Δx = π/40. Step 3: generate the vector y using...
scheme: Write a recursive Scheme function (subst x y L), which returns a list identical to...
scheme: Write a recursive Scheme function (subst x y L), which returns a list identical to L except that every occurrence of x has been replaced with y. The following example illustrates the use of this function: > (subst 'c 'k '(c o c o n u t)) (k o k o n u t) Write a recursive Scheme function (all-different? L), which determines whether all elements of list L are distinct (that is, not equal?). The following example illustrates...
PLEASE USE PYTHON CODE Compute the zero of the function y(x) from the following data: x...
PLEASE USE PYTHON CODE Compute the zero of the function y(x) from the following data: x = 0.2, 0.4, 0.6, 0.8, 1.0 y = 1.150, 0.855, 0.377, -0.266, -1.049 Use inverse interpolation with the natural cubic spline
For the X,Y data below, compute:
For the X,Y data below, compute: X Y 2 5 4 6 4 7 5 11 6 12 (a) the correlation coefficient and determine if it is significantly different from zero. (b) find the regression line. c) interpret the coefficient of the regression line d) at 5% test if the Y is related to X e) find the coefficient of determination and interpret this coefficient. f) predict the value of average Y if the average of X is13 g) at...
Develop an x86 assembly language program that properly executes an "x to the y power" function:...
Develop an x86 assembly language program that properly executes an "x to the y power" function: int power(int x, int y) Compute the integer value that is x to the y power Do not concern yourself with the possibility of overflow You may only use only a loop structure to compute the value Remember that the return value must be placed in the EAX register Make sure that any registers (not EAX) used by this function are placed back to...
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers....
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers. If x=2, y=4, the method pow should return 16. Java answers only please.
Implement and complement the recursive code to compute the product of two positive integers, x and...
Implement and complement the recursive code to compute the product of two positive integers, x and y, using only addition and/or subtraction. The function will take as its arguments two integers to multiply together ( x x y ) and will return the product. Hint: consider the following: 6 x 1 = 6 6 x 2 = 6 + (6 x 1) 6 x 3 = 6 + (6 x 2) = 6 + [6 + (6 x 1)] =...
Develop a well-structured MATLAB function to compute the Maclaurin series expansion for the cosine function and...
Develop a well-structured MATLAB function to compute the Maclaurin series expansion for the cosine function and name the function cosMac. The function should have the following features: Iterate until the relative error (variable name “relErr” ) falls below a stopping criterion OR exceeds a maximum number of iterations (variable name“maxIter”) for a given value of x. Include a default value of relErr = 1e-6 if the user does not enter the value (use nargin function). Include a default value for...
Compute the area under y = √x between x = a and x = b where...
Compute the area under y = √x between x = a and x = b where a and b are user specified values obtained via cin. Account for invalid user input cases of a < 0 and a > b. For each case of invalid input, immediately output to the user what the error was. Allow the user a total of three chances to enter valid input for each input request. If the user enters incorrect input three times in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT