`
xitonga
  • 浏览: 579410 次
文章分类
社区版块
存档分类
最新评论

DP17 最少回文切割次数 Palindrome Partitioning @geeksforgeeks

 
阅读更多

Given a string, a partitioning of the string is apalindrome partitioningif every substring of the partition is a palindrome.For example, “aba|b|bbabb|a|b|aba” is a palindrome partitioning of “ababbbabbababa”. Determine the fewest cuts needed for palindrome partitioning of a given string. For example, minimum 3 cuts are needed for “ababbbabbababa”. The three cuts are “a|babbbab|b|ababa”. If a string is palindrome, then minimum 0 cuts are needed. If a string of length n containing all different characters, then minimum n-1 cuts are needed.

Solution
This problem is a variation ofMatrix Chain Multiplicationproblem. If the string is palindrome, then we simply return 0. Else, like the Matrix Chain Multiplication problem, we try making cuts at all possible places, recursively calculate the cost for each cut and return the minimum value.

Let the given string be str and minPalPartion() be the function that returns the fewest cuts needed for palindrome partitioning. following is the optimal substructure property.

// i is the starting index and j is the ending index. i must be passed as 0 and j as n-1
minPalPartion(str, i, j) = 0 if i == j. // When string is of length 1.
minPalPartion(str, i, j) = 0 if str[i..j] is palindrome.

// If none of the above conditions is true, then minPalPartion(str, i, j) can be 
// calculated recursively using the following formula.
minPalPartion(str, i, j) = Min { minPalPartion(str, i, k) + 1 +
                                 minPalPartion(str, k+1, j) } 
                           where k varies from i to j-1

Following is Dynamic Programming solution. It stores the solutions to subproblems in two arrays P[][] and C[][], and reuses the calculated values.


动态规划一言以蔽之,就是要找最合算的方案


package DP;

public class PalindromePartitioning {

	public static void main(String[] args) {
		String s = "ababbbabbababa";
//		String s = "aa";
		int begin = 0;
		int end = s.length()-1;
//		System.out.println(isPalindromeRec(s, begin, end));
		System.out.println(minPalPartitionDP(s));
		System.out.println(minPalPartitionRec(s, begin, end));
	}
	
	public static boolean isPalindromeRec(String s, int begin, int end){
		if(begin > end){
			return false;
		}
		if(begin == end){
			return true;
		}
		if(end==begin+1 && s.charAt(begin)==s.charAt(end)){
			return true;
		}
		return s.charAt(begin)==s.charAt(end) && isPalindromeRec(s, begin+1, end-1);
	}
	
	public static int minPalPartitionRec(String s, int begin, int end){
		if(begin == end){		// string长度为1时,肯定是,所以无需切割
			return 0;
		}
		if(isPalindromeRec(s, begin, end)){	// 如果s是回文,也无需切割
			return 0;
		}
		int min = Integer.MAX_VALUE;
		for(int k=begin; k<end; k++){	// 找到最合算的切割点
			min = Math.min(min, minPalPartitionRec(s, begin, k)+
										minPalPartitionRec(s, k+1, end)+1);
		}
		return min;
	}
	
	
	// Returns the minimum number of cuts needed to partition a string
	// such that every part is a palindrome
	public static int minPalPartitionDP(String s){
		int n = s.length();		// Get the length of the string
		
		/* Create two arrays to build the solution in bottom up manner
	       minCuts[i][j] = Minimum number of cuts needed for palindrome partitioning
	                 of substring s[i..j]
	       pld[i][j] = true if substring s[i..j] is palindrome, else false
	       Note that C[i][j] is 0 if P[i][j] is true */
		int[][] minCuts = new int[n][n];
		boolean[][] pld = new boolean[n][n];
		
		// Every substring of length 1 is a palindrome
		for(int i=0; i<n; i++){
			minCuts[i][i] = 0;
			pld[i][i] = true;
		}
		
		 /* l is substring length. Build the solution in bottom up manner by
	       considering all substrings of length starting from 2 to n.
	       The loop structure is same as Matrix Chain Multiplication problem (
	       See http://www.geeksforgeeks.org/archives/15553 )*/
		for(int l=2; l<=n; l++){
			// For substring of length L, set different possible starting indexes
			for(int begin=0; begin<n-l+1; begin++){
				int end = begin+l-1;		// Set ending index
				// If L is 2, then we just need to compare two characters. Else
	            // need to check two corner characters and value of P[i+1][j-1]
				if(l == 2){
					pld[begin][end] = (s.charAt(begin) == s.charAt(end));
				}else{
					pld[begin][end] = (s.charAt(begin)==s.charAt(end) && pld[begin+1][end-1]);
				}
				
				if(pld[begin][end] == true){	// IF s[i..j] is palindrome, then minCuts[i][j] is 0
					minCuts[begin][end] = 0;
				}else{
					// Make a cut at every possible location starting from i to j,
	                // and get the minimum cost cut.
					minCuts[begin][end] = Integer.MAX_VALUE;
					for(int k=begin; k<=end-1; k++){
						minCuts[begin][end] = Math.min(minCuts[begin][end], minCuts[begin][k]+minCuts[k+1][end]+1);
					}
				}
			}
		}
		// Return the min cut value for complete string. i.e., s[0..n-1]
		return minCuts[0][n-1];
	}

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics