Palindromic Length
Time limit0.5sMemory limit2048 MB
Given a lowercase string of up to 100,000 letters, find the fewest palindromic substrings it can be split into with no overlaps.
- Level
Medium7 of 10
- Topics
- Dynamic programming, String, String matching
- Solved
- No attempts yet
Problem
A string is called a palindrome if it reads the same forward and backward. Palindromes are useful for measuring the complexity of strings, such as their asymmetry.
The asymmetry of a string of length can be measured by its palindromic length, . This is the minimum number of palindromic substrings into which can be partitioned. More precisely, is the minimum () such that there exist palindromic substrings whose concatenation equals . A partition of into is written as | | | .
For example, abaaca can be partitioned into two palindromic substrings as aba | aca, and two is the minimum, so for abaaca. The string acaba cannot be split into two palindromes, but it can be split into three: aca | b | a or a | c | aba, so for acaba. For radar, is itself a palindrome, so . For abcde, .
Input
The first line contains a positive integer (), the number of letters in the string. The next line contains a string of English lowercase letters. The letters are not separated by spaces.
Output
Print exactly one line containing the palindromic length of the input string . The value is a positive integer.