Question

Consider two strings A="qpqrr"A = \text{"qpqrr"} and B="pqprqrp"B = \text{"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 = _____.

Official answer and solution
Official answer
34
From
Official GATE 2014 answer key

Solution

Determine the Longest Common Subsequence length using dynamic programming and enumerate distinct maximal subsequences.

  1. Given Strings:

    A="qpqrr"(∣A∣=5),B="pqprqrp"(∣B∣=7).A = \text{"qpqrr"} \quad (|A| = 5), \qquad B = \text{"pqprqrp"} \quad (|B| = 7).
  2. Finding LCS Length xx:
    Constructing the DP table for lengths:

    • Subsequences of length 4:
      • Check qprr: in AA, indices are 0,2,3,40, 2, 3, 4 (q, q, r, r →\to with matching q at 0, p at 1, r at 3, r at 4); in BB, q at 1, p at 2, r at 3, r at 5. Match found!
      • Check qpqr: in AA, q(0), p(1), q(2), r(3); in BB, q(1), p(2), q(4), r(5). Match found!
      • Check pqrr: in AA, p(1), q(2), r(3), r(4); in BB, p(0), q(1), r(3), r(5). Match found!
    • Can we form length 5? The only length-5 subsequence of AA is AA itself (qpqrr). Looking for qpqrr in BB: first q is at index 1, next p at index 2, next q at index 4, next r at index 5. But BB has no second r after index 5! So length 5 is impossible.
    • Thus, maximum length x=4x = 4.
  3. Enumerating Distinct LCS of Length 4 (yy):
    The 4-character subsequences of AA are:

    • Dropping index 0 (q): pqrr (present in BB)
    • Dropping index 1 (p): qqrr (not in BB, since BB has only one q before r)
    • Dropping index 2 (q): qprr (present in BB)
    • Dropping index 3 (r): qpqr (present in BB)
    • Dropping index 4 (r): qpqr (duplicate)
      Thus, there are exactly 3 distinct LCS strings: {"qprr", "qpqr", "pqrr"}.
    y=3.y = 3.
  4. Computing x+10yx + 10y:

    x+10y=4+10(3)=4+30=34.x + 10y = 4 + 10(3) = 4 + 30 = 34.
Final answer

34

Common trap

Counting identical subsequence strings multiple times due to multiple index paths rather than distinct strings.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2014 Computer Science and Information Technology (CS) Session 2
Question
47 of 65, printed as CS Q.37
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.