Palindrome Partitioning Cost

HARD
G
Time limit: 2.0sMemory limit: 256MB
dpstringspalindrome

Palindrome Partitioning Cost

Given a lowercase string s, you may cut it into pieces such that every ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​‌‌​​​‌​​‌‌​​​​‌​‌‌​​‌​​​‌‌​​‌​‌​​‌‌​‌​​​​‌‌​​‌​​‌‌​​​​‌​‌‌​​‌​‌​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​‌‌​​‌‌‌​​‌​​‌‌​‌​​​​‌‌​​‌​piece is a palindrome. Find the minimum number of cuts needed (a string that is already a palindrome needs 0 cuts).

Input

A single ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​‌‌​​​‌​​‌‌​​​​‌​‌‌​​‌​​​‌‌​​‌​‌​​‌‌​‌​​​​‌‌​​‌​​‌‌​​​​‌​‌‌​​‌​‌​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​‌‌​​‌‌‌​​‌​​‌‌​‌​​​​‌‌​​‌​line: the string s (1 ≤ |s| ≤ 2000, lowercase letters only)

Output

A single integer: the minimum number of cuts.

Example

Input:

aab

Output:

1

​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​‌‌​​​‌​​‌‌​​​​‌​‌‌​​‌​​​‌‌​​‌​‌​​‌‌​‌​​​​‌‌​​‌​​‌‌​​​​‌​‌‌​​‌​‌​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​‌‌​​‌‌‌​​‌​​‌‌​‌​​​​‌‌​​‌​Explanation: cut into aa and b.

Constraints

  • 1 ≤ |s| ≤ 2000 ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​‌‌​​​‌​​‌‌​​​​‌​‌‌​​‌​​​‌‌​​‌​‌​​‌‌​‌​​​​‌‌​​‌​​‌‌​​​​‌​‌‌​​‌​‌​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​‌‌​​‌‌‌​​‌​​‌‌​‌​​​​‌‌​​‌​

Sample Testcases

Case 1

Input
aab
Expected Output
1

Case 2

Input
a
Expected Output
0
Loading Cloud IDE...
Ln 1, Col 1Spaces: 4UTF-8
pythonAuto-Saved

Input

aab

Expected Output

1

Run or submit your code to see output here.