GATE / CS ARCHIVE

GATE CSE 2014 SET 2 / QUESTION 37

GATE CSE 2014 Set 2 Q37

Theory of Computation · Other / Mixed Topics · Full GATE CSE 2014 Set 2 paper

GATE 2014 Set 2, Q37 2 marks · NAT
Report issue

Other / Mixed Topics

Question 37, GATE 2014: For A="qpqrr" and B="pqprqrp", let x be the length of a longest common subsequence, not necessarily contiguous, and y the number of distinct such longest subsequence strings. Find x+10y. The source says “number of such longest common subsequences”; the distinct-string convention is used by the keyed result.
Tap to enlarge
Answer & solution

Official answer: 34

Step through it (37 steps)

1. Set up the reasoning. Try to embed the entire five-letter A in B first. After choosing q,p,q in order in B, only one trailing r remains, so no length-five common subsequence exists. Then enumerate the distinct strings obtained by deleting one character from A.

2. Derive the result. The longest common subsequence length for qpqrr and pqprqrp is 4. The distinct length 4 common strings are pqrr, qpqr, qprr. The entire 5-letter first string is not a subsequence of the second, so no longer one exists. Therefore x+10y=4+30=34, counting distinct subsequence strings rather than embeddings.

3. Check the interpretation. Deleting the first, second, or third character gives pqrr, qqrr, qprr; deleting either final r gives qpqr. Of these, qqrr is not a subsequence of B, leaving three distinct length-four strings. Repeated embeddings of one string are not counted separately.

Original paper, page 39 · Answer key