Data Structures & AlgorithmsVery hardCodeforces Div2 D · TCS NQT
The string "abaccbbca" is split into contiguous palindromic pieces. A piece of length L costs L². What is the minimum total cost?
- A. 9
- B. 13
- C. 10
- D. 8
Correct answer: A. 9
dp[j] = min over palindromic s[i..j−1] of dp[i] + (j − i)²: a single palindromic block of length L costs L², so cutting into many short palindromes is often cheaper than one long one, and the DP over all cut points handles the trade-off exactly.
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 maximum sum of a contiguous subarray of [4, -1, 2, 2, 5, -3, 4, 1] that contains at most 2 distinct values?
- What is the minimum number of characters you must delete from "abbabaab" (keeping the order of the rest) so that the result contains NO…
- How many ordered pairs (i, j) with i ≠ j exist in [2, 4, 3, 8, 6, 9] such that a[j] is divisible by a[i]?
- From the stick lengths [4, 2, 7, 5, 3, 6, 9], choose three different sticks that can form a non-degenerate triangle (the sum of any two…
- How many integers x with 1 ≤ x < 300 have the same decimal digit sum as x + 1?
- Prices [3, 8, 2, 9, 1, 7] are listed for six days. You pick two different days, buy on one and sell on the other, with no time-order…