Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
Let L be the length of the longest common subsequence of "ABCBDAB" and "BDCABA". How many different index-selections in "ABCBDAB" give a common subsequence of length L (each selection is a set of positions, and two selections with the same letters but different positions count separately)? Count index sets of the first string that spell an LCS which also occurs in the second string.
- A. 4
- B. 7
- C. 1
- D. 5
Correct answer: A. 4
Track both the length and the number of ways for each prefix pair: when letters match, extend the diagonal count; when they do not, add the counts of the tied neighbours. Reporting only the LCS length answers a different question.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the length of the longest common subsequence of "dbacabb" and its cyclic rotation by one position (the string with its first…
- Using only insertions and deletions (no substitutions), what is the minimum number of operations needed to turn "heat" into "hit"?
- How many permutations of the numbers 1 to 6 have a longest increasing subsequence of length exactly 3?
- How many permutations of 1, 2, 3, 4, 5 have EXACTLY 4 inversions? An inversion is a pair of positions i < j with p[i] > p[j].
- Piles of stones [3, 5, 1, 2, 6] are in a row. Each move merges two ADJACENT piles into one, at a cost equal to their combined number of…
- In how many different ways can the product of 5 matrices A·B·C·D·E be fully parenthesised (grouping by brackets, keeping the order of…