3
// function to precompute if every substring of s is a palindrome or not4
vector<vector<bool>> isPalindrome(string &s) {6
vector<vector<bool>> dp(n, vector<bool>(n, false));8
for (int i = 0; i < n; i++) {12
for (int i = 0; i < n - 1; i++) {13
if (s[i] == s[i + 1]) {25
if (s[i] == s[j] and dp[i + 1][j - 1]) {39
// function to find the minimum palindromic substrings in s40
int solve(string &s, int n, int i, vector<vector<bool>> &palin, vector<int> &memo) {50
for (int j = i + 1; j <= n; j++) {51
if (palin[i][j - 1]) {52
result = min(result, 1 + solve(s, n, j, palin, memo));56
return memo[i] = result;59
int minCut(string s) {61
vector<int> memo(n, -1);62
vector<vector<bool>> palin = isPalindrome(s);63
return solve(s, n, 0, palin, memo) - 1;