Day title series: Palindrome Partitioning II

Source: Internet
Author: User

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.

Description excerpt from ref http://blog.csdn.net/ljphhj/article/details/22573983

Problem Solving Ideas:

We can turn this problem into a dynamically programmed DP problem.

First, we define a few variables, and do some of these quantities to explain! To facilitate understanding, these are pseudo-code!!!

Len = Str.length (); The length of the string

Int[] cuts = new Int[len + 1]; Cuts array, Cuts[i] indicates that a substring beginning with I to Len ends up to the minimum number of cuts required by test instructions (so that the final cuts[0] is the result we want) "Initialize cuts[i" = Len-i, because the worst case is that the substring that starts with I to Len ends up cutting the number Each character is cut once "

int[][] Matrix = new Int[len][len]; Sets an array of adjacency matrices, meaning that it represents: matrix[i][j] = True, which indicates that the sub-string sub (I, j) satisfies the palindrome string condition!

The criteria for determining whether matrix[i][j] satisfies a string of characters is:

Matrix[i+1][j-1] = = True (indicates sub (i+1,j-1) is satisfies palindrome string) && str[i] = = Str[j]

Or

J-i < 2 && str[i] = = Str[j] (that is, if j-i = = 1 o'clock, two characters are equal, if j-i = = 0 o'clock, the same character)

In both cases, we set matrix[i][j] to true to facilitate the next DP, and we can find the minimum number of cuts

Cuts[i] = Min{cuts[i], cuts[j+1] + 1}; State transition equation

So last cuts[0]-1 will be the smallest cut number of string STR!!!!

Here the cut is initialized more specifically

 Public classSolution { Public intmincut (String s) {if(s==NULL|| S.length () <2)return0; intLen =s.length (); int[] Cut =New int[Len+1]; Boolean[] Matrix =New Boolean[Len][len];  for(inti=0; i<len;i++) {Cut[i]= Len-i;//Not len-i-1        }         for(inti=len-1;i>=0;i--){             for(intj=i;j<len;j++){                if((S.charat (i) ==s.charat (j)) && ((j-i<2) | | Matrix[i+1][j-1])) {Matrix[i][j]=true; Cut[i]= Math.min (cut[i],cut[j+1]+1); }            }        }        returnCut[0]-1; }}

Day title series: Palindrome Partitioning II

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.