Question

LCS (Longest Common Subsequence) problem consists in finding the longest subsequence of characters that is present...

 

LCS (Longest Common Subsequence) problem consists in finding the longest subsequence of characters that is present in two given sequences in the same order.

A subsequence is a sequence of characters that don't have to be consecutive.

For example, for the following inputs, the length of the LCS is 4

X: ABCBDAB

Y: BDCABA

And one of the LCS is BDAB

The following code is a solution for such problem, which values must be stored in the variable key for it to properly work?

Inputs: X and Y are strings to be analysed. n is the number of characters in X. m is the number of characters in Y. table is a dictionary with the solutions to the subproblems.

Output: The maximum number of characters in a LCS of X and Y

function LCS(X, n, Y, m, table)

    if n < 0 or m < 0

        return 0

    if key not in table

        if X[n] == Y[m]

            table[key] = LCS(X, n-1, Y, m-1, table) + 1

        else

            in_x = LCS(X, n-1, Y, m, table)

            in_y = LCS(X, n, Y, m-1, table)

            table[key] = max(in_x, in_y)

    

    return table[key]

a) X and Y

b) n

c) m

d) m and n

e) The base cases

Which is the correct answer?

Homework Answers

Answer #1

Option (d) is the correct answer.

The values of m and n (length of both strings) must be stored in the variable key for the proper working of the code. At first, we must check if the end for either sequence has reached. If not, then we must check if the last character of the strings matches. We also need to check the condition if they don't match.

Option (a) is incorrect as we need to store the string lengths, not the strings.

Option (b) is incorrect as both m and n must be stored.

Option (c) is incorrect as both m and n must be stored.

Option (e) is incorrect as the base case will be the not null check.

Please comment in case of any doubt.
Please upvote if this helps.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Complete a function definition in C for strmatch. For this problem, you can't use any <string.h>...
Complete a function definition in C for strmatch. For this problem, you can't use any <string.h> library functions. In c, two strings match exactly if they have the same characters in the same order and same length. Consider this: int strmatch(const char *x, const char *b); //Needs each of x and y points to the beginning of a string. //Promises to return value 1 if the two strings match exactly. If they don't match, the return value is 0. For...
The following algorithm finds the initial substring of y that can be reversed and found in...
The following algorithm finds the initial substring of y that can be reversed and found in y. For example, longestInitialReverseSubstringLength(“aabaa”) = 5, because “aabaa” is the same string forwards and backwards, so the longest initial substring that can be reversed and found in the string is “aabaa”. Also, longestInitialReverseSubstringLength(“bbbbababbabbbbb”) is 6, because “babbbb” can be found in the string (see color-highlighted portions of the string), but no longer initial string exists reversed in any part of the string. longestInitialReverseSubstringLength(String y)...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
Provide a complete solution to the following problem using the C++ language in a SINGLE file...
Provide a complete solution to the following problem using the C++ language in a SINGLE file with a .cpp file extension. READ THE ENTIRE QUESTION (AND THE EXAMPLE) CAREFULLY BEFORE DEVELOPING YOUR SOLUTION! Design the code for a class called Pi, which will be used to encapsulate the value of pi stored as a string. (example ONLY) "3.141592654" | |_ _ _ _| whole number portion_| |_ _ _ _ _fractional portion (9 digits) The Pi class has the following...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Problem 1 A simple harmonic oscillator consists of a block (m = 0.50 kg) attached to...
Problem 1 A simple harmonic oscillator consists of a block (m = 0.50 kg) attached to a spring (k = 128 N/m). The block is pulled a certain distance from the equilibrium position and released at t = 0 s. The block slides on a horizontal frictionless surface about the equilibrium point x = 0 m with a total mechanical energy of 16 J. a) What are the amplitude and phase constant of the oscillation? (4 pts.) b) Find the...
Problem 1: Relations among Useful Discrete Probability Distributions. A Bernoulli experiment consists of only one trial...
Problem 1: Relations among Useful Discrete Probability Distributions. A Bernoulli experiment consists of only one trial with two outcomes (success/failure) with probability of success p. The Bernoulli distribution is P (X = k) = pkq1-k, k=0,1 The sum of n independent Bernoulli trials forms a binomial experiment with parameters n and p. The binomial probability distribution provides a simple, easy-to-compute approximation with reasonable accuracy to hypergeometric distribution with parameters N, M and n when n/N is less than or equal...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from a file. Specifically, you will implement the following function: /* * Reads one arithmetic "expression" at a time from a file stream, computes, then * returns the result. If there are additional expressions in the file, they are * read and computed by successive calls to “calculator”. * * “Expressions” are groups of operations (add, subtract, multiply, divide). Your * calculator will read and...
python programming Question #4: # Years ago the Romans used a different system to represent numbers....
python programming Question #4: # Years ago the Romans used a different system to represent numbers. # Instead of using the digits (0, 1, 2, 3, 4, 5, 6, etc.), the Romans # formed numbers by joining combinations of the characters # (I, V, X, L, C, D, and M). # Roman Numeral characters and their integer values are: # I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, and M...