[LeetCode]Palindrome Partitioning 找出所有可能的回文组合

给定一个串,分割该串,使得每个子串都是回文串。找出所有可能的组合。

方法:暴搜+回溯

class Solution {
public:
	int *b,n;
	vector<vector<string> >ans;
	void dfs(int id,string &s,int len){
		if(id>=n){
			if(len>0){
				vector<string>vt;
				vt.push_back(s.substr(0,b[0]+1));
				for(int i=1;i<len;++i){
					vt.push_back(s.substr(b[i-1]+1,b[i]-b[i-1]));
				}
				ans.push_back(vt);
			}
			return;
		}
		int j,k;
		b[len]=id;
		dfs(id+1,s,len+1);
		for(j=id+1;j<n;++j){
			for(k=0;id+k<j-k&&s[id+k]==s[j-k];++k);
			if(id+k>=j-k){
				b[len]=j;
				dfs(j+1,s,len+1);
			}
		}
	}
	vector<vector<string> > partition(string s) {
		n=s.size();
		if(n==0)return ans;
        if(n==1){
            vector<string>vt;
            vt.push_back(s);
			ans.push_back(vt);
			return ans;
		}
		b=new int[n];
		dfs(0,s,0);
		return ans;
    }
};
时间: 2024-08-04 04:10:34

[LeetCode]Palindrome Partitioning 找出所有可能的回文组合的相关文章

最长(大)回文串的查找(字符串中找出最长的回文串)PHP实现

首先还是先解释一下什么是回文串:就是从左到右或者从右到左读,都是同样的字符串.比如:上海自来水来自海上,bob等等. 那么什么又是找出最长回文串呢? 例如:字符串abcdefedcfggggggfc,其中efe,defed,cdefedc,gg,ggg,gggg,ggggg,gggggg,fggggggf,cfggggggfc都是回文串,左右完全一样. 这其中,有最短的gg,最长的cfggggggfc,还有其他长度的.忽略长度为1的.毕竟一个字符的都算回文了. 那么,找出最长的,就是找出这个cf

[leetcode]Palindrome Partitioning @ Python

原题地址:https://oj.leetcode.com/problems/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 [

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

[LeetCode] Palindrome Partitioning II [12]

题目 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

LeetCode: Palindrome Partitioning II [132]

[题目] 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] 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] Palindrome Partitioning II 拆分回文串之二

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: Palindrome Partitioning II 解题报告

Palindrome Partitioning II 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 pa

[LeetCode] Palindrome Partitioning II 解题笔记

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