【leetcode】Palindrome Partitioning && Palindrome Partitioning II

Palindrome Partitioning


鏈接: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

  [
    ["aa","b"],
    ["a","a","b"]
  ]

解法:
dfs搜索所有解,同時藉助dp[i][j]計算從i個字符到第j個字符是否迴文。

代碼如下:
    vector<vector<string>> partition(string s) {
    	vector<vector<string>> result;
    	int len  = s.length();
    	if( len <= 0) return result;
    	vector<vector<bool> > dp(len, vector<bool>(len, false));
    	for(int i = 0; i < len; ++i)
    		dp[i][i] = true;
    
    	for(int r = 2; r <= len; ++r)
    	{
    		for(int i=0; i <= len - r;++i)
    		{
    			int j = i+r-1;
    			if( s[i] == s[j] && ( i+1 >= j-1 || dp[i+1][j-1]) )
    				dp[i][j] = true;
    		}
    	}
    
    	vector<string> path;
    	dfs(s, 0, dp, path, result);
    	return result;
    }
    
    void dfs(string &s, int start, vector<vector<bool>> &dp,
			vector<string> &path, vector<vector<string>> &result)
    {
    	int len = s.length();
    	if(start == len)
    	{
    		result.push_back(path);
    		return;
    	}
    
    	for(int i=start; i < len; ++i)
    	{
    		if( dp[start][i] ){
    			path.push_back(s.substr(start, i - start + 1));
    			dfs(s, i+1, dp, path, result);
    			path.pop_back();
    		}
    	}
    }


Palindrome Partitioning II

鏈接:https://oj.leetcode.com/problems/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","b"] could be produced using 1 cut.


方法一:
兩個DP,結果爲了圖省事,寫了一個O(n^3)。超時

代碼如下:
int minCut(string s)
{
	int len = s.length();
	if( len <= 1) return 0;
	vector<vector<int>> dp(len, vector<int>(len, INT_MAX));
	for(int i=0;i < len; ++i)
		dp[i][i] = 0;

	for(int r=2; r <= len; ++r)
	{
		for(int i=0; i <= len - r;++i)
		{
			int j = i + r - 1;
			if(s[i] == s[j] && (i+2 >= j || dp[i+1][j-1] == 0)){
				dp[i][j] = 0;
				continue;
			}
			for(int m=i; m < j; ++m)
			{
				int temp = 1 + dp[i][m] + dp[m+1][j];
				if( dp[i][j] > temp)
					dp[i][j] = temp;
			}
		}
	}
	return dp[0][len-1];
}


方法二:
拆分DP,將第二個DP轉化爲一位DP。
DP[i] = 0 if  flag[0][i] == true 
DP[i] =  min {  DP[j] + flag[j+1][i] ? 1 : (i-j) }  0 <= j < i;  else
進一步分析 當flag[j+1][i] 爲真時更新就能保證正確性。
代碼如下:

int minCut2(string s)
{
	int len = s.length();
	if(len <= 1) return 0;
	vector<vector<bool>> dp(len, vector<bool>(len, false));
	for(int i=0;i < len; ++i)
		dp[i][i] = true;

	for(int r=2; r <= len; ++r)
	{
		for(int i=0; i <= len-r; ++i)
		{
			int j = r + i - 1;
			if( s[i] == s[j] && (i + 2 >= j || dp[i+1][j -1]))
				dp[i][j] = true;
		}
	}

	vector<int> output(len, 0);
	for(int i=1;i < len; ++i)
	{
		if( dp[0][i] ){
			output[i] = 0;
			continue;
		}else{
			output[i] = i;
		}
		for(int j = i; j > 0; --j)
		{
			if( dp[j][i] )
				output[i] = min(output[i], output[j-1] + 1);
		}
	}
	return output[len-1];
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章