leetcode解析回文子串拆分

转载请注明来自souldak,微博:@evagle

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...i的拆分已经都知道了,用C[i]表示,现在考虑s[i+1],

1. s[i+1]自己分为一组,C[i+1]中加入C[i]中每一种拆分加上s[i+1]的结果

2. s[i+1]与前k个构成回文,即s[i+1]s[i]...s[i-k+1]是回文,那么C[i+1]加入C[k]中的每种拆分加上s[i+1]s[i]...s[i-k+1]的结果

最终就得到了C[i+1]

举个例子:aab,现在C[0]={{a}}

现在看C[1].分两种情况,s[1]单独一个字符串{a,a},s[1]和s[0]构成回文{aa}。

C[1]={{a,a},{aa}}

C[2]类似,b自己单独,那就是C[1]中的每个集合加入b,得到{{a,a,b},{aa,b}}

再检测b与前k个字符是否构成回文,发现不能构成回文了,所以最终结果就是{{a,a,b},{aa,b}}

CODE:

/**
 * @file Palindrome_Partitioning.cpp
 * @Brief 
 * @author  Brian 
 * @version 1.0
 * @date 2013-09-06
 */

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
 
#define MAXINT 0x7fffffff
 
class Solution {
    public:
        struct Node{
            vector< vector<string> > vstr ;
        };
        vector<vector<string> > partition(string s) {

            int len = s.length();
            Node* nodes  = new Node[len];

            vector<string> str0;
            str0.push_back(s.substr(0,1));
            nodes[0].vstr.push_back(str0);

            for(int i=1;i<len;i++){
                for(int j=0;j<=i;j++){
                    string subs = s.substr(j,i-j+1);
                    if(is_palindrome(subs)){
                        if(j==0){
                            vector<string> tmp;
                            tmp.push_back(subs);
                            nodes[i].vstr.push_back(tmp);
                        }else{
                            for(int k=0;k<nodes[j-1].vstr.size();k++){
                                vector<string> tmp;
                                vector<string> pre = nodes[j-1].vstr[k];
                                for(int p=0;p<pre.size();p++){
                                    tmp.push_back(pre[p]);
                                }
                                tmp.push_back(s.substr(j,i-j+1));
                                nodes[i].vstr.push_back(tmp);  
                            }
                        }
                    }

                }
            }
            return nodes[len-1].vstr;
        }
        bool is_palindrome(string s){
            if(s.length() <= 1)
                return true;
            else{
                for(int i=0;i<s.length()/2;i++){
                    if(s[i]!=s[s.length()-i-1]){
                        return false;
                    }
                }    
            }
            return true;

        }
        void printv (vector<vector<string> > str){
            for(int i=0;i<str.size();i++){
                for(int j=0;j<str[i].size(); j++){
                    cout<<str[i][j]<<"\t";
                }
                cout<<endl;
            }
        }
};
int main(){
    Solution s;
    s.printv(s.partition("a"));
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,有三种方法可以解决LeetCode上的最长回文子串问题。 方法一是使用扩展中心法优化,即从左向右遍历字符串,找到连续相同字符组成的子串作为扩展中心,然后从该中心向左右扩展,找到最长的回文子串。这个方法的时间复杂度为O(n²)。\[1\] 方法二是直接循环字符串,判断子串是否是回文子串,然后得到最长回文子串。这个方法的时间复杂度为O(n³),效率较低。\[2\] 方法三是双层for循环遍历所有子串可能,然后再对比是否反向和正向是一样的。这个方法的时间复杂度也为O(n³),效率较低。\[3\] 综上所述,方法一是解决LeetCode最长回文子串问题的最优解法。 #### 引用[.reference_title] - *1* [LeetCode_5_最长回文子串](https://blog.csdn.net/qq_38975553/article/details/109222153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Leetcode-最长回文子串](https://blog.csdn.net/duffon_ze/article/details/86691293)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [LeetCode 第5题:最长回文子串(Python3解法)](https://blog.csdn.net/weixin_43490422/article/details/126479629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值