标签:
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]; }
标签:
原文地址:http://blog.csdn.net/li_chihang/article/details/43453641