Consider two strings = "qpqrr" and = "pqprqrp". Let be the length of the longest common subsequence (not necessarily contiguous) between and and let be the number of such longest common subsequences between and . Then ___.
GATE CSE 2014Algorithms › Greedy Method, Dynamic Programming & Algorithm Design TechniquesVerified
GATE CSE 2014 PYQ: Consider two strings A A A = "qpqrr" and B B B = "pqprqrp" . Let x x x be the length of the longest common…
Answer
34
Approach
Since A has length 5, any common subsequence of length 4 must be obtained by deleting exactly one character from A. Enumerate all distinct length-4 subsequences of A, test each against B to find which are valid, then identify the maximum length x and the number y of distinct longest common subsequences.
The longest common subsequence length is 4, and there are exactly 3 distinct longest common subsequences. So the required value is 4 + 10*3 = 34.
Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.