zoukankan      html  css  js  c++  java
  • 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思路,假设D[n]为每个位置的minCut,从后往前开始统计, D[i] = D[j+1]+1, if s[i,j]为回文串,利用二维数组来记录s[i,j]是不是回文串

    class Solution {
    public:
        int minCut(string s) {
            int n= s.size();
            if(n==0)
            return 0;
            vector<int> D(n);
            vector<vector<bool> > matrix(n,vector<bool>(n,false));
            for(int i=0; i<n; i++)
                D[i] = n-1-i;
            for(int i = n-1; i>=0; i--){
                for(int j=i; j<n; j++){
                    if(s[i]==s[j] && (j-i<2 || matrix[i+1][j-1])){
                        matrix[i][j] = true;
                        if(j == n-1)
                            D[i] = 0;
                        else
                            D[i] = min(D[i], D[j+1]+1);
                    }
                }
            }
            return D[0];
        }
    };
  • 相关阅读:
    flex产生水平滚动条
    js中的类
    typescript
    vue练习
    vue-cli2脚手架搭建
    Luogu P1970 花匠
    Luogu P1311 选择客栈
    Luogu P1016 旅行家的预算
    Luogu P1144 最短路计数
    Luogu P1091 合唱队形
  • 原文地址:https://www.cnblogs.com/willwu/p/6395490.html
Copyright © 2011-2022 走看看