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

题意理解:就是把一个字符串进行切割,要求切割之后的子串是回文串。

思路步骤:1.回文字符串划分 2.动态规划生成回文字符串数组 3.根据dp数组用深度搜索生成回文字符串的划分

简单描述一下,首先用动态规划的方法记录出dp[i][j]是否为回文子串(是为1,否则为0)。dp[i][j]表示字符串s中的索引从i....j的子串是不是回文字符串。

构造dp数组,当i=j时,dp[i][j]=1。

当i不等于j时,要求dp[i][j]只需当s[i]==s[j]且dp[i+1][j-1]=1来判断其余的即可。(i+1和j-1表示子串s[i...j]变为子串s[i+1...j-1],即去掉左右两边)

因此我们得反着来求dp,因为需要用到i+1.

然后根据生成好的dp数组,用dfs对数组进行划分。

代码:
class Solution {
private:
    int dp[200][200];
    vector<vector<string>> result;
    void dfs(string s, int begin,vector<string> temp) {
        if(begin==s.length()) {
            result.push_back(temp);
            return;
        }
        for(int i=begin;i<s.length();i++) {
            if(dp[begin][i]==1) {
                temp.push_back(s.substr(begin,i-begin+1));
                dfs(s,i+1,temp);
                temp.pop_back();
            }
        }
    }
    void dp_resolve(string s){
        int n=s.size();
        memset(dp,0,sizeof(dp));

        for (int i = n-1; i >=0; --i)
        {
            for (int j = i; j < n; ++j)
            {
                if(j==i){
                    dp[i][j]=1;
                }else if(j==i+1){
                    if(s[i]==s[j]) dp[i][j]=1;
                }
                else{
                    if(s[i]==s[j]&&dp[i+1][j-1]) dp[i][j]=1;
                }
            }
        }
        vector<string> temp;
        dfs(s,0,temp);
        return;
    }
public:
    vector<vector<string>> partition(string s) {
        if(s.empty()) return result;
        dp_resolve(s);
        return result;
    }
};

参考博文:http://blog.csdn.net/worldwindjp/article/details/22042133

     http://blog.csdn.net/u011095253/article/details/9177451

 类似题目:最长回文子串(Longest Palindromic Substring)                最长回文子序列
时间: 2024-11-20 17:25:15

Palindrome Partitioning (回文子串题)的相关文章

[C++]LeetCode: 121 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",&

URAL 1297. Palindrome(输出最长回文子串--后缀数组)

Input The input consists of a single line, which contains a string of Latin alphabet letters (no other characters will appear in the string). String length will not exceed 1000 characters. Output The longest substring with mentioned property. If ther

【后缀数组|最长回文子串】URAL-1297 Palindrome

1297.Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just received a rather alarming anonymous letter. It states that the agent from the competing ?Robots Unlimited? has infiltrated into "U.S. Robotics".

URAL-1297 Palindrome (最长回文子串)

Palindrome Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrat

【算法】最长回文子串 longest palindrome substring

对于字符串S, 要找到它最长的回文子串,能想到的最暴力方法,应该是对于每个元素i-th都向左向右对称搜索,最后用一个数组span 记录下相对应元素i-th为中心的回文子串长度. 那么问题来了: 1. 这样的方法,对于奇回文子串和偶回文子串的处理不一样,比如所"acbca" 和"acbbca" 2. 计算冗余,e.g. "sscssabcccchccccba"中, 自左向右遍历计算的话,会发现, "abcccchccccba"是

后缀数组 - 求最长回文子串 + 模板题 --- ural 1297

1297. Palindrome Time Limit: 1.0 secondMemory Limit: 16 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots» s

算法题之最大回文子串

题目描述对于一个字符串,请设计一个高效算法,计算其中最长回文子串的长度.给定字符串A以及它的长度n,请返回最长回文子串的长度. 测试样例:"abc1234321ab",12 返回:7 1. 普通轮询(运行时间80ms): class Palindrome { public: bool isHuiWen(string A, int n){ int k = n / 2; for (int i = 0; i < k; ++i) { if (A.at(i) != A.at(n - 1 -

算法题--最长回文子串

题目描述 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设?s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: "aba" 也是一个有效答案. 示例 2: 输入: "cbbd" 输出: "bb" 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/longest-palindromic-substring

Ural 1297 Palindrome 【最长回文子串】

最长回文子串 相关资料: 1.暴力法 2.动态规划 3.中心扩展 4.Manacher法 http://blog.csdn.net/ywhorizen/article/details/6629268 http://blog.csdn.net/kangroger/article/details/37742639 在看后缀数组的时候碰到的这道题目 不过没用后缀数组写出来= = 用了一个很暴力的做法卡进了1 s Source Code: //#pragma comment(linker, "/STAC