problem:https://leetcode.com/problems/palindrome-partitioning-ii/
Climb stairs type of problem. First calculate all possible palindromic sequence, DP [i] i represents the minimum divided front characters, strings found in all i palindromic ends, whichever is the smallest division that as a result.
class Solution { public: int minCut(string s) { int n = s.size(); vector<vector<bool>> palindrome(n, vector<bool>(n, false)); for(int i = 0;i < n;i++) { palindrome[i][i] = true; for(int times = 0; times < 2; times++) { int j; if(times == 0) j = i - 1; else j = i; int k = i + 1; while(j >= 0 && k < n) { if(s[j] == s[k]) { palindrome[j][k] = true; j--; k++; } else break; } } } vector<int> dp(n + 1); dp[0] = -1; for(int i = 0;i < n; i++) { dp[i + 1] = i; for(int j = 0;j <= i; j++) { if(palindrome[j][i]) { dp[i + 1] = min(dp[i + 1], dp[j] + 1); } } } return dp[n]; } };