GFG App
Open App
Browser
Continue

# GATE | GATE-CS-2014-(Set-2) | Question 47

Consider two strings A = \”qpqrr\” and B = \”pqprqrp\”. Let x be the length of the longest common subsequence (not necessarily contiguous) between A and B and let y be the number of such longest common subsequences between A and B. Then x + 10y = ___.

(A)

33

(B)

23

(C)

43

(D)

34

Explanation:

//The LCS is of length 4. There are 3 LCS of length 4 \”qprr\”, \”pqrr\” and qpqr   A subsequence is a sequence that can be derived from another sequence by selecting zero or more elements from it, without changing the order of the remaining elements. Subsequence need not be contiguous. Since the length of given strings A = â€śqpqrrâ€ť and B = â€śpqprqrpâ€ť are very small, we donâ€™t need to build a 5×7 matrix and solve it using dynamic programming. Rather we can solve it manually just by brute force. We will first check whether there exist a subsequence  of length 5 since min_length(A,B) = 5. Since there is no subsequence , we will now check for length 4. â€śqprrâ€ť, â€śpqrrâ€ť and â€śqpqrâ€ť are common in both strings. X = 4 and Y = 3 X + 10Y = 34   This solution is contributed by Pranjul Ahuja

Quiz of this Question
Please comment below if you find anything wrong in the above post

My Personal Notes arrow_drop_up