码迷,mamicode.com
首页 > 其他好文 > 详细

Palindrome Partitioning

时间:2015-02-03 15:09:45      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://blog.csdn.net/li_chihang/article/details/43449191

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!