Even though it was poorly written and tle, it was right. It's still worth remembering.
Class Solution {Public:int Pcut (string s, int start, int end) {if (Start >= end) return 0;
Stack<tuple<int, int>> Pre;
Tuple<int, int> tmp;
int i = 0, I2, J;
int fir, sec, overlap;
for (i = start; i < end; i++) {i2 = i;
while (I2 + 1 < end && S[i2] = = S[i2 + 1]) i2++;
j = 0;
while (I-j-1 >= start && i2 + j + 1 < end && S[i-j-1] = = S[i2 + j + 1]) J + +;
if (!pre.empty ()) {tmp = Pre.top ();
If included in the previous palindrome, skip directly if (I2 + j <= get<1> (tmp)) {i = i2; continue;
}//Find the previous palindrome contained in the current palindrome and delete the while (get<0> (TMP) >= i-j) {pre.pop ();
if (Pre.empty ()) {break;
} TMP = Pre.top ();
} if (!pre.empty ()) {fir = get<0> (tmp), sec = get<1> (TMP); Handle overlapping portions if (Fir < i-j && sec >= i-j) {int min1 = Pcut (S, start, i-j) + pcut (s, I- J, end);//Shrink last palindrome int min2 = Pcut (S, start, sec + 1) + PCUT (S, sec + 1, end);//Shrink current palindrome return min1 < min2?
Min1 + 1:min2 + 1;
}}} Pre.push (Make_tuple (i-j, I2 + j));
i = i2;
} return Pre.size ()-1;
} int Mincut (string s) {if (S.length () <= 1) return 0;
Return Pcut (S, 0, s.length ()); }
};
Even though it was poorly written and tle, it was right. It's still worth remembering.