Skip to main content

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…

Consider two strings AA = "qpqrr" and BB = "pqprqrp". Let xx be the length of the longest common subsequence (not necessarily contiguous) between AA and BB and let yy be the number of such longest common subsequences between AA and BB. Then x+10y=x +10y= ___.

    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.