Problem 534
Count Palindrome Partitions
Medium · 20 pts · String, Dynamic Programming
Problem
Count the ways to cut a string S into consecutive pieces such that every piece is a palindrome (reads the same forwards and backwards). Two ways are different if they cut at a different set of positions. Print the count modulo 1000000007. First mark which substrings are palindromes in O(N²); then ways[i], the number of ways to split the first i characters, is the sum of ways[j] over every j where S[j..i-1] is a palindrome. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).
Input
A single string S of lowercase English letters.
Output
Print the number of palindrome partitions modulo 1000000007.
Constraints
1 <= |S| <= 2000
Example input
aab
Example output
2
Explanation
aab can be cut as a | a | b or as aa | b. Any other cut leaves ab or aab as a piece, which are not palindromes, so the answer is 2.