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

Palindrome Partitioning II

时间:2015-02-03 21:27:35      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

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","b"] could be produced using 1 cut.

解题思路:动态规划.

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

int minCut(string s) {
	int Len = static_cast<int>(s.size());
	vector<bool>tmp(Len, false);
	vector<vector<bool>>BePalindrome(Len, tmp);
	vector<int>ResultCount(Len+1, -1);
	for (int i = Len-1; i >=0;--i)
	{
		ResultCount[i] = Len;
		for (int j = i; j <= Len - 1;++j){
			if (s[i]==s[j]&&(i+1>=j||BePalindrome[i+1][j-1])){//如果子串i到j是回文
				BePalindrome[i][j] = true;
				ResultCount[i] = min(ResultCount[j+1]+1, ResultCount[i]);//则i开始的子串分割数为两者最小.
			}
		}
	}
	return ResultCount[0];
}


Palindrome Partitioning II

标签:

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

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