Given a stringS, PartitionSSuch that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioningS.
For example, givenS="aab"
, Return1
Since the palindrome partitioning["aa","b"]
Cocould be produced using 1 cut.
Avoid using recursion to produce inefficient programs. Use the DP Method:
Class solution {public: int mincut (string s) {If (S. empty () return 0; int n = S. size (); vector <bool> pal (n, vector <bool> (n, false )); // record whether the input vector <int> D (n) is formed between Si and SJ ); // record Si to S end split can form the smallest number of split back to the text for (INT I = n-1; I> = 0; I --) {d [I] = n-i-1; for (Int J = I; j <n; j ++) {If (s [I] = s [J] & (J-I <2 | pal [I + 1] [J-1]) {pal [I] [J] = true; If (j = N-1) d [I] = 0; else if (d [J + 1] + 1 <D [I]) d [I] = d [J + 1] + 1 ;}}} return d [0] ;}};
Each pair takes into account the O (N ^ 2) number of times, and then only considers the sub-sequence in which the sub-sequence can constitute the Back-to-text, that is, the sub-sequence pal [I] [J] = true can be formed between Si and SJ.