首页 > 代码库 > Palindrome Partitioning

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"]
  ]
解题思路:很简单的一题判定回文的题,利用递归调用的方式输出每种可能.

#include<iostream>
#include<vector>
#include<string>
using namespace std;

bool JudgeIsPalindrome(string s)
{
	for (int first = 0, last = s.size() - 1; first <= last;++first,--last){
		if (s[first] == s[last])
			continue;
		else
			return false;
	}
	return true;
}
void RecurisonSplit(string &S, vector<string> &Result, vector<vector<string> >&ResultVector)
{
	if (S.empty()){
		ResultVector.push_back(Result);
		return;
	}
	for (int len = 1; len <= S.size();++len)
	{
		if (JudgeIsPalindrome(S.substr(0, len))){
			Result.push_back(S.substr(0, len));
			RecurisonSplit(S.substr(len), Result, ResultVector);
			Result.pop_back();
		}
	}
}
vector<vector<string>> partition(string s) {
	vector<vector<string> >ResultVector;
	vector<string>Result;
	RecurisonSplit(s, Result, ResultVector);
	return ResultVector;
}



Palindrome Partitioning