【Palindrome Partitioning】cpp

题目:

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

Return all possible palindrome partitioning of s.

For example, given s = "aab",
Return

  [
    ["aa","b"],
    ["a","a","b"]
  ]

代码:

class Solution {
public:
    vector<vector<string>> partition(string s) {
            vector<vector<string> > ret;
            vector<string> tmp;
            Solution::dfs(ret, tmp, s, 0, s.size()-1);
            return ret;
    }
    static void dfs(vector<vector<string> >& ret, vector<string>& tmp, string& s, int begin, int end)
    {
            if ( begin>end ) { ret.push_back(tmp); return; }
            for ( int i = begin; i <= end; ++i )
            {
                if ( Solution::isPalindrome(s, begin, i) )
                {
                    tmp.push_back(s.substr(begin,i-begin+1));
                    Solution::dfs(ret, tmp, s, i+1, end);
                    tmp.pop_back();
                }
            }
    }
    static bool isPalindrome(string& s, int begin, int end)
    {
            while ( begin<end && s[begin]==s[end] ) { begin++; end--; }
            return begin>=end;
    }
};

tips:

把问题转化为深搜:字符串s有n个字符,因此可以有n个切的位置(包括不切)。

按照深搜的模板写出来代码(dfs终止条件;深搜遍历条件等)。

注意一个细节,传入下一层dfs时是从i+1到end,之前一直写成了begin+1犯了低级错误。

深搜的时间复杂度为O(2^n) 空间复杂度为O(1)。

深搜在这道题上也有不足之处,很多子字符串是否是回文算过了不止一遍,自然联想能否用动规算法保存每一轮的判断结果。

====================================================

先往后走,后面再研究动态规划的做法。

时间: 2024-10-21 07:05:59

【Palindrome Partitioning】cpp的相关文章

【Palindrome Number】cpp

题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using

【palindrome partitioning II】cpp

题目: Given a string s, partition s such that every substring of the partition is 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&qu

【Scramble String】cpp

题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choo

【Word Break】cpp

题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true becau

【Sudoku Solver】cpp

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 代码: cla

【Subsets II】cpp

题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,2], a sol

【LRU Cache】cpp

题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【Rotate List】cpp

题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 代码: /** * Definition for singly-linked list. * struct ListNode {

【Text Justification】cpp

题目: Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pa