Question

Write a pseudocode description of the printLCS () algorithm, which prints the longest common subsequence of two strings x and

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Following is algorithm to print the LCS. It uses the same 2D table L[][].

There are two strings X and Y
length of X is m
length of Y is n
1) Construct L[m+1][n+1] using the steps of finding the matrix for length of lcs.

2) The value L[m][n] contains length of LCS. Create a character array lcs[] of length equal to the length of lcs plus 1 (one extra to store \0, to end the char array.).

2) Traverse the 2D array starting from L[m][n]. Do following for every cell L[i][j]

   a) If characters (in X and Y) corresponding to L[i][j] are same (Or X[i-1] == Y[j-1]), then include this character as part of LCS.

   b) Else compare values of L[i-1][j] and L[i][j-1] and go in direction of greater value.

Code to print the longest common sub sequence of two Strings X and Y.

#this method returns the matrix of lcs
def lcs(X, Y, m, n):
L = [[0 for x in range(n+1)] for x in range(m+1)]
  
# Following steps build L[m+1][n+1] in bottom up fashion. Note
# that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1]
for i in range(m+1):
for j in range(n+1):
if i == 0 or j == 0:
L[i][j] = 0
elif X[i-1] == Y[j-1]:
L[i][j] = L[i-1][j-1] + 1
else:
L[i][j] = max(L[i-1][j], L[i][j-1])

return L

#this method prints the LCS.
def print_lcs(L, X, Y):
  
m = len(X)
n = len(Y)
# Following code is used to print LCS
index = L[m][n]
  
# Create a character array to store the lcs string
lcs = [""] * (index+1)
lcs[index] = ""
  
# Start from the right-most-bottom-most corner and
# one by one store characters in lcs[]
i = m
j = n
while i > 0 and j > 0:
  
# If current character in X[] and Y are same, then
# current character is part of LCS
if X[i-1] == Y[j-1]:
lcs[index-1] = X[i-1]
i-=1
j-=1
index-=1
  
# If not same, then find the larger of two and
# go in the direction of larger value
elif L[i-1][j] > L[i][j-1]:
i-=1
else:
j-=1
  
print("LCS of " + X + " and " + Y + " is " + "".join(lcs))
  
# Driver program
X = "Shashank Shukla"
Y = "Surbhi Sharma"
m = len(X)
n = len(Y)

L = lcs(X, Y, m, n)
print_lcs(L, X, Y)


Add a comment
Know the answer?
Add Answer to:
Write a pseudocode description of the printLCS () algorithm, which prints the longest common subs...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write the algorithm pseudocode for the longest common subsequence problem using dynamic programming. What is...

    1. Write the algorithm pseudocode for the longest common subsequence problem using dynamic programming. What is its running time?

  • Need help with all 3 parts. Thanks Question 1 (Longest Common Subsequence) In the longest common...

    Need help with all 3 parts. Thanks Question 1 (Longest Common Subsequence) In the longest common sub- sequence algorithm we discussed in class, we formulated the recursive formula based on prefixes of the two inputs, i.e., X[1...) and Y [1..,]. 1. Rewrite the recursive formula using suffixes instead of prefixes, i.e., X[...m] and Y[j..n]. 2. Develop a bottom-up dynamic programming algorithm based on the recur- sive formula in (a). Describe the algorithm and write a pseudo code. 3. Use the...

  • Can I please get help with this? will upvote upon completion. Problem 2. The longest common...

    Can I please get help with this? will upvote upon completion. Problem 2. The longest common substring problem is to find the longest string that is a substring of two strings. The longest common substring of the strings "ABABC", and "ABCBA" is string "ABC" of length 3. A substrings of strings is a series of consecutive letters of s. For example, "ABA” is a substring of “ABABC", but "ABAC" is not a substring of "ABABC". Design an algorithm such that...

  • A. Write an algorithm in pseudocode, which prints all strings of length 6 where the first...

    A. Write an algorithm in pseudocode, which prints all strings of length 6 where the first three characters are any symbol from X={!,@,#,$,%,^}, the 4th and 5th characters are any digit 0,1,…9, and the last character is any digit 1,2,…9. How many times will the print command be called (e.g. how many 6-character strings will be printed)? B. Let g(n)=n3 + 2n2 - 5. Determine the big-theta estimate of f. A complete response will include analysis of: , and ....

  • 1 Problem Description Instructions. You are provided one skeleton program named LCS.java . The source files...

    1 Problem Description Instructions. You are provided one skeleton program named LCS.java . The source files are available on Canvas in a folder named HW6 . Please modify the skeleton code to solve the following tasks. • Task 1 (100 pts). Implement the lcs length() function as discussed in Lecture 11. • Note: You should not return the double-array b and c as in the pseu- docode. Instead, return the length of the longest common subsequence. • Hint: To get...

  • Given two strings X and Y, a third string Z is a common superstring of X...

    Given two strings X and Y, a third string Z is a common superstring of X and Y if X and Y are both subsequences of 2. (Example: if X = sos and Y = soft, then Z = sosft is a common superstring of X and Y.) Design a dynamic programming algorithm which, given as input two strings X and Y, returns the length of the shortest common superstring (SCS) of X and Y. Specifically, you have to write...

  • Question 2 (2.5 points) Longest common subsequence Consider the procedure to determine the length...

    Question 2 (2.5 points) Longest common subsequence Consider the procedure to determine the length of the longest common subsequence, LCS- LENGTH(X, Y). It solves the LCS problem in a bottom-up manner, by filling out a 2-D tabular LCS-LENGTH(X, Y) m = X.length 2. n-Y.length 3. let cO.m, 0n] be a new array 4, for i = 0 to m for/ = 0 to n else if ATY ci,ci ,j-1 +1 10 else 12. return clm, n] For example, X (B,...

  • I need this using C++. In this project, you will implement the dynamic programming-based solution to...

    I need this using C++. In this project, you will implement the dynamic programming-based solution to find the longest common subsequence (LCS) of two sequences. Your inputs will be the two sequences (as Strings) and the outputs are the longest common subsequence (printed as a String) and the final matrix (printed as a two-dimensional array) depicting the length of the longest common subsequences (as shown in the slides) for all possible subsequences of the two input sequences. The two input...

  • In this project, you will work on the algorithm (discussed in Module 1) to determine the...

    In this project, you will work on the algorithm (discussed in Module 1) to determine the length of the longest sub sequence of consecutive integers in an array You will implement the algorithm using Hash tables. You are provided with sample code (in C++ and Java) representing the linked list-based implementation of Hash tables (as an array of Linked Lists). You could go through the code to understand the implementation of a Hash table and the functions that can be...

  • Hello I have an automata question could you help me? [1Points] Give a formal description of a Tu...

    Hello I have an automata question could you help me? [1Points] Give a formal description of a Turing Machine M  that takes two parameters: an integer and an array of integers and decides whether the given integer is an element of the array or not. You can assume that all the integers are between 0 and 9. The input string will be written on the tape of the Turing machine. The first square of the tape contains the integer, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT