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?

  1. A. 9
  2. B. 13
  3. C. 10
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic