Palindromic Partitions
Time limit10sMemory limit128 MB
Split a string into chunks so the chunk sequence is a palindrome, and report the maximum number of chunks possible.
- Level
Medium7 of 10
- Topics
- String, Greedy, Hash map, Prefix sum
- Solved
- No attempts yet
Problem
A partition of a string is a set of one or more non-overlapping non-empty substrings of whose concatenation is , that is . Each substring is called a chunk, and the length of the partition is the number of chunks .
A partition is written by putting every chunk in parentheses. For example, the string decode can be partitioned as (d)(ec)(ode), (d)(e)(c)(od)(e), (decod)(e), (decode), (de)(code), and in many other ways.
A partition is palindromic if the sequence of chunks reads the same forwards and backwards when each chunk counts as one atomic unit. The only palindromic partitions of decode are (de)(co)(de) and (decode). The second one shows that every word has a palindromic partition of length one.
Given a word , compute the largest possible length of a palindromic partition of .
Input
The first line contains the number of test cases . Each of the next lines holds one test case: a single word made of lowercase letters of the English alphabet. The input contains no spaces.
Output
For every test case print one line with the length of the longest palindromic partition of the word .
Constraints
Let be the length of the word .