[Leetcode]132.palindrome Partitioning II

Source: Internet
Author: User

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

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.