回文串划分 Palindrome Partition

此题为动态规划。 问题描述看这里

转移方程式如下

// dp[i] = min{ dp[i - k] if (s[k] == s[i]) && (s[k+1..i-1] is palindrome) }, k = 0,1,2...i. dp[i]的初始值是一个很大的值

注意:任何重复判断回文串的方案都会造成超时。必须要缓存回文串判断的结果。

//.h

#include <string>

class PalindromePartition {
public:
    static void test();

    static int minCut(std::string &s);

    static bool isPalindrome(const std::string &s);

};

//.cpp
//
// Created on 3/14/18.
//

#include <iostream>
#include <vector>
#include "PalindromePartition.h"

using namespace std;

// dp[i] = min{dp[i - k] if (s[k] == s[i]) && (s[k+1..i-1] is palindrome) }, k = 0,1,2...i. dp[i]的初始值是一个很大的值
int PalindromePartition::minCut(std::string &s) {
    const int N = s.size();

    if (isPalindrome(s)) {
        return 0;
    }

    vector<vector<int>> mat(N, vector<int>(N, 0));

    for (int i = 0; i < N; i++) {
        mat[i][i] = 1;
    }

    vector<int> dp(N, 0);
    for (int i = 0; i < N; i++) {
        dp[i] = 0x0fffffff;
        for (int j = 0; j <= i; j++) {
            if ((s[j] == s[i])) {
                if (j + 1 <= i - 1) {
                    if (!mat[j+1][i-1]) {
                        continue;
                    }
                }
                mat[j][i] = 1;
                if (j - 1 >= 0) {
                    dp[i] = min(dp[i], dp[j - 1] + 1);
                } else {
                    dp[i] = 0;
                }
            }
        }

    }
    return dp[N - 1];
}

bool PalindromePartition::isPalindrome(const std::string &s) {
    if (s.size() <= 1) {
        return true;
    }

    const int N = s.size();
    for (int i = 0; i < N / 2; i++) {
        if (s[i] != s[N - i - 1]) {
            return false;
        }
    }
    return true;
}

void PalindromePartition::test() {
    vector<string> vec = {
            "abbab",
            "aab",
            "abccba",
            "abcabc",
            "aaabaaa",
            "aabcaa",
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    };
    for (auto &s: vec) {
        cout << minCut(s) << endl;
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 题目:Longest Palindromic Substring Given a string s, find t...
    Arnold134777阅读 4,840评论 0 0
  • 新年之际,给大家带来了一道题目并且解决方法。希望大家喜欢 题目: 给定字符串,找到它的最长回文子串,都有哪些思路呢...
    kiaizi阅读 3,325评论 0 1
  • 题目 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串。 返回s所有可能的回文串分割方案。 样例给出 s...
    六尺帐篷阅读 7,196评论 2 2
  • 1、情人时常有,但是你真心爱的人并不一定会常常有 我是刚刚下载简书这个软件的,也看啦几篇这方面的文章,看到有很多被...
    靛簟阅读 2,713评论 3 1
  • 孤单是一种心理反应,独自一人时、有很多人在周围时都可能会有这种感觉。孤单常被视为是人类痛苦最普遍的来源之一,每个人...
    静静_ee7f阅读 3,110评论 1 1

友情链接更多精彩内容