Topic
Given a string s, partition s such that every substring of the partition are a palindrome.
Return the minimum cuts needed for a palindrome partitioning of S.
For example, given s = "AaB",
Return 1 since the palindrome partitioning ["AA", "B"] could be produced using 1 cut.
Ideas
This problem can be solved by dynamic programming. Ispal[j][i] Indicates the minimum number of segments required for substring S[J...I] to be a palindrome string, cut[i] to denote substring s[0...i]
Code
/**------------------------------------* Date: 2015-03-02 * SJF0115 * Title: 132.Palindrome Partitioning II * URL: https://oj.leetcode.com/problems/palindrome-partitioning-ii/* result: AC * Source: Leetcode * Blog:----- ----------------------------------**/ #include <iostream> #include <vector> #include <cstring> #include <algorithm> using namespace STD;classSolution { Public:intMincut (strings) {intSize = S.size ();if(Size = =0){return 0; }//if //ISPAL[I][J] Indicates whether the substring of string s s[i,j] is a palindrome BOOLIspal[size][size];memset(Ispal,0,sizeof(Ispal));///CUT[J] The minimum number of divisions required to represent substring S[0,j] intCut[size];//Cut[0,i] for(inti =0; i < size;++i) {//[0,i] up to split I timesCut[i] = i;//Judge S[j,i] Whether it is a palindrome string for(intj =0; J <= I;++j) {//S[j,i] is a palindrome string if(S[j] = = S[i] && (i-j <=1|| ispal[j+1][i-1]) {Ispal[j][i] =true;//S[0,i] is a palindrome string if(J = =0) {Cut[i] =0; }//if Else{Cut[i] = min (cut[i],cut[j-1]+1); }//else}//if}//for}//for returncut[size-1]; } };intMain () {solution S;stringStr"CABABABCBC");cout<<s.mincut (str) <<endl;return 0; }
Run time
[Leetcode]132.palindrome Partitioning II