Data Structures & AlgorithmsHard
For counting subsequences equal to a fixed pattern (like 'abc'), why must the DP update the longer prefixes first?
- A. Updating in any order gives the same result, so it does not matter.
- B. Updating longer prefixes first prevents one character from extending a prefix that it already created in the same step.
- C. The DP needs the pattern to be sorted.
- D. Longer prefixes must be cleared before the scan starts.
Correct answer: B. Updating longer prefixes first prevents one character from extending a prefix that it already created in the same step.
When a character matches pattern position k, the count for prefix k must use the old count for prefix k−1, so the update runs from the end of the pattern backwards. Otherwise one character would be used twice.
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
- Why does the count of subsequences (index subsets) with sum divisible by m use a DP over residues rather than enumerating subsets?
- Why is an XOR-zero subset family structured like a vector space over the two-element field?
- When counting subarrays with sum in a range [L, R] for an array with zeros, why can a single subarray start be counted many times with a…
- Why does the lexicographically smallest arrangement with a 'no fixed point' constraint start with the value 2?
- When enumerating subsets, what is the total count of non-empty subsets of an n-element set?
- Why does the count of subsequences with sum divisible by m give the same answer if you replace each value by its residue mod m?