131.132. Palindrome Partitioning *hard*--Split palindrome string

Source: Internet
Author: User

131. Palindrome Partitioning

Given A string s, partition s such that every substring of the partition are a palindrome.

Return all possible palindrome partitioning of s.

For example, given s = "aab" ,
Return

[  ["AA", "B"],  ["A", "a", "B"]]
classSolution { Public:    BOOLIspalindrome (strings) {intL =s.length (), left, right;  for(left =0, right = L1; Left < right; left++, right--)        {            if(S[left]! =S[right])return false; }        return true; }    voidPartitionhelper (vector<vector<string>> &ans,string&s,intStart, vector<string> &VEC) {        intL =s.length (), I; if(Start = =l) {ans.push_back (VEC); return; }         for(i = start; I < L; i++)        {            stringSub = s.substr (Start, i-start+1); if(Ispalindrome (sub)) {Vec.push_back (sub); Partitionhelper (ans, s, I+1, VEC);            Vec.pop_back (); }}} vector<vector<string>> partition (strings) {vector<vector<string>>ans; Vector<string>VEC; Partitionhelper (ans, S,0, VEC); returnans; }};

Palindrome Partitioning II

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.

classSolution { Public:    intMincut (strings) {intL =s.length (), I, J; if(L <=1)            return 0;

Determines whether a palindrome string vector<vector<BOOL>> Ispal (L, vector<BOOL> (L,false)); for(i = l-1; I >=0; i--) //here {ispal[i][i]=true; for(j = i+1; J < L; J + +) { if(S[i] = = S[j] && (j = = i+1|| ispal[i+1][j-1])) Ispal[i][j]=true; }} vector<int>num (l); num[0] =0; for(i =1; I < L; i++) { if(ispal[0][i]) {Num[i]=0; Continue; } Num[i]=i; for(j =1; J <= I; J + +) { if(Ispal[j][i] && num[j-1]+1<Num[i]) num[i]= num[j-1] +1; } } returnnum[l-1]; }};

(1)

Construct the Pailndrome checking matrix
1) Matrix[i][j] = true; if (i==j)--only one Char
2) Matrix[i][j] = true; if (i==j+1) && S[i]==s[j]--only and chars
3) Matrix[i][j] = matrix[i+1][j-1]; If S[I]==S[J]--more than, chars

Attention:

When constructing a matrix, it is necessary to move from bottom to top, otherwise the values used in some locations are not filled in.

(2)

/*
* Dynamic Programming
* -------------------
*
* Define Res[i] = The minimum cut from 0 to I in the string.
* The result eventually is Res[s.size ()-1].
* We know res[0]=0. Next We is looking for the optimal solution function f.
*
* For example, let S = "leet".
*
* F (0) = 0; Minimum cut of str[0:0]= "L", which is a palindrome, so isn't cut is needed.
* F (1) = 1; Str[0:1]= "Le" How to get 1?
* F (1) might is: (1) F (0) +1=1, the minimum cut before plus the current char.
* (2) 0, if Str[0:1] is a palindrome (here "le" isn't)
* F (2) = 1; Str[0:2] = "Lee" How to get 2?
* F (2) might be: (1) F (1) + 1=2
* (2) 0, if Str[0:2] is a palindrome (here "Lee" isn't)
* (3) F (0) + 1, if Str[1:2] is a palindrome, yes!
* F (3) = 2; Str[0:3] = "leet" How to get 2?
* F (3) might be: (1) F (2) + 1=29
* (2) 0, if Str[0:3] is a palindrome (here "leet" are not)
* (3) F (0) + 1, if Str[1:3] is a palindrome
* (4) F (1) + 1, if STR[2:E] is a palindrome (this "et" is not)
* OK, Output F (3) =2 as the result.
*
* So, the optimal function is:
*
* F (i) = min [F (j) +1, J=0..i-1 and Str[j:i] is palindrome
* 0, if str[0,i] is palindrome]
*
* The above algorithm works well for the smaller test cases, however for the big cases, it still cannot pass.
* Why? The "the" we test the palindrome is time-consuming.
*
* Also using the similar DP idea, we can construct the look-up table before the main part above,
* So, the palindrome testing becomes the looking up operation. The construct the table is also the idea of DP.
*
* e.g. mp[i][j]=true if STR[I:J] is palindrome.
* MP[I][I]=TRUE;
* Mp[i][j] = True if STR[I]==STR[J] and (Mp[i+1][j-1]==true or j-i<2) j-i<2 ensures the array boundary.
*/

131.132. Palindrome Partitioning *hard*--Split palindrome string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.