Palindrome Partitioning[leetcode] DFS以及DP的解法

第一种方法是DFS,将所有可能的前缀找到,递归调用partition(剩余字符串)

复杂度为O(2^n)

代码如下:

    vector<vector<string>> partition(string s) {
        vector<vector<string>> res;
        vector<string> patition;
        if (s.size() == 0) return res;
        partition(s, patition, res);
        return res;
    }

    void partition(string s, vector<string>& patition, vector<vector<string>>& res) {
        for (int i = 1; i < s.size(); i++)
        {
            if (isPalindrome(s.substr(0, i)))
            {
                patition.push_back(s.substr(0, i));
                partition(s.substr(i), patition, res);
                patition.pop_back();
            }
        }
        if (isPalindrome(s))
        {
            patition.push_back(s);
            res.push_back(patition);
            patition.pop_back();
        }
    }

    bool isPalindrome(string s)
    {
        int l = 0, r = s.size() - 1;
        while (l <= r)
        {
            if (s[l] != s[r]) return false;
            l++;
            r--;
        }
        return true;
    }

第二种方法是DP

来自 https://oj.leetcode.com/discuss/9623/my-java-dp-only-solution-without-recursion-o-n-2

res(i)表示s[0...i-1]的所有分解方式

isPalin(i,j)表示s[i...j]是否为回文串

isPalin(i,j) = true  if  i==j or (s[i] == s[j] and isPalin(i + 1, j - 1)) or (s[i] == s[j] and i + 1 == j)

res(i) = res(j) + s[j...i-1]  if isPalin(j, i-1)

    vector<vector<string>> partition(string s) {
        int size = s.size();
        vector<vector<vector<string>>> res(size + 1);
        res[0].push_back(vector<string>(0));
        vector<vector<bool>> isPalin(size + 1, vector<bool>(size + 1, false));

        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j <= i; j++)
            {
                if (i == j || s[i] == s[j] && (j + 1 == i || isPalin[j + 1][i - 1]))
                {
                    isPalin[j][i] = true;
                    for (int p = 0; p < res[j].size(); p++)
                    {
                        vector<string> prefix = res[j][p];
                        prefix.push_back(s.substr(j, i - j + 1));
                        res[i + 1].push_back(prefix);
                    }
                }
            }
        }
        return res[size];
    }
时间: 2024-12-29 07:03:54

Palindrome Partitioning[leetcode] DFS以及DP的解法的相关文章

Palindrome Partitioning leetcode java

题目: 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",&

Palindrome Partitioning -- leetcode

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","

Palindrome Partitioning——LeetCode

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","

Atcoder Yet Another Palindrome Partitioning(状压dp)

Atcoder Yet Another Palindrome Partitioning 思路: 一个字符串满足条件的情况是奇数字母个数小于等于1,也就是异或起来是1<<j(0<=j<=25) 记mark是异或起来的值 状态转移: dp[mark]=dp[mark]+1; dp[mark]=min(dp[mark^(1<<j)]+1,dp[mark]);(0<=j<=25) 注意dp[0]被转移后可能会变成1,但是由它转移的需要dp[0]=0,所以每次记得把d

[LeetCode] Palindrome Partitioning II (DP)

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"

leetcode dfs Palindrome Partitioning

Palindrome Partitioning Total Accepted: 21056 Total Submissions: 81036My Submissions 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 = "a

LeetCode: Palindrome Partitioning 解题报告

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",

Palindrome Partitioning II Leetcode java

题目: 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&q

lightoj-1044 - Palindrome Partitioning(区间dp)

1044 - Palindrome Partitioning PDF (English) Statistics ForumTime Limit: 1 second(s) Memory Limit: 32 MBA palindrome partition is the partitioning of a string such that each separate substring is a palindrome. For example, the string "ABACABA" c