天题系列: 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.

说明摘抄自ref http://blog.csdn.net/ljphhj/article/details/22573983

解题思路:

我们可以把这个问题转换成动态规划dp的问题

 

首先我们先定义几个变量,并对这几个量做一定的说明!为了方便理解,下面这些为伪码!!!

len  = str.length();   // 字符串的长度

int[] cuts = new int[len + 1]; //cuts数组,cuts[i] 表示 以 i 开头到len结尾的子串 要达到题意需要的最少切割数(这样子最终 cuts[0]就是我们要的结果)【初始化 cuts[i] = len - i, 因为最坏情况以 i 开头到len结尾的子串要切割数就是每个字符都切一次】

int[][] matrix = new  int[len][len]; //设置出一个邻接矩阵数组,它所表示的意思:如matrix[i][j] = true, 表示 子串 sub(i, j) 是满足回文字符串条件的!

那么判断matrix[i][j] 是否满足回文字符串的条件是: 

 

matrix[i+1][j-1] == true (表示sub(i+1,j-1)是满足回文字符串) && str[i] == str[j] 

或者 

j - i < 2 && str[i] == str[j] (即如果j - i == 1时,为两个字符相等,如果j - i == 0时,为同一个字符) 

 

这两种情况,我们都将matrix[i][j]设置成true,方便下一次的DP,并且我们可以求出最小的切割次数

cuts[i] = min{cuts[i], cuts[j+1] + 1};  状态转移方程式

这样最后cuts[0] - 1便为 字符串str的最小的切割数!!!!

这里cut的初始化比较特别

public class Solution {
    public int minCut(String s) {
        if(s==null||s.length()<2) return 0;
        int len = s.length();
        int [] cut = new int[len+1];
        boolean[][] matrix = new boolean[len][len];
        for(int i=0; i<len;i++){
            cut[i] = len-i; // not len-i-1 
        }
        for(int i=len-1;i>=0;i--){
            for(int j=i;j<len;j++){
                if((s.charAt(i)==s.charAt(j)) &&((j-i<2)||  matrix[i+1][j-1])){
                    matrix[i][j] = true;
                    cut[i] = Math.min(cut[i],cut[j+1]+1);
                }
            }
        }
        return cut[0]-1;
    }
}

 

posted @ 2015-06-11 13:25  世界到处都是小星星  阅读(133)  评论(0编辑  收藏  举报