https://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"]
]
class Solution {
public:
void Parlindrome(vector<vector<bool> >& isPal, string& s) {
for(int i=;i<isPal.size();++i) {
for(int j=;j<=i;++j) {
isPal[i][j] = true;
}
} for(int len=;len<=s.length();++len) {
for(int i=;i+len-<s.length();++i) {
int j=i+len-;
if(i==j || (s[i]==s[j] && isPal[i+][j-])) isPal[i][j] = true;
}
} }
void dfs(vector<vector<string> >& res, vector<string>& load, vector<vector<bool> >& isPal, string& s, int idx) {
if(idx == s.length()) {
res.push_back(load);
return;
} for(int nx=idx;nx<s.length();++nx) {
if(isPal[idx][nx]) {
load.push_back(s.substr(idx, nx-idx+));
dfs(res, load, isPal, s, nx+);
load.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
vector<vector<bool> > isPal(s.length(), vector<bool>(s.length(), false));
vector<string> load; load.clear();
vector<vector<string> > res; Parlindrome(isPal, s);
dfs(res, load, isPal, s, );
return res;
}
};

leetcode 131: Palindrome Partitioning

https://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.

class Solution {
public:
void Parlindrome(vector<vector<bool> >&isPal, string& s) {
for(int i=;i<s.length();++i) isPal[i][i-] = true;
for(int len=;len<=s.length();++len) {
for(int i=;i+len-<s.length();++i) {
int j=i+len-;
if(i==j || (s[i]==s[j] && isPal[i+][j-])) isPal[i][j] = true;
}
}
}
void minCut(vector<vector<bool> >& isPal, vector<int>& cut, string& s) {
for(int i=;i<s.length();++i) {
if(isPal[][i] == true) {
cut[i] = ;
continue;
} for(int pre=;pre<i;++pre) {
if(isPal[pre+][i]) {
cut[i] = min(cut[pre]+, cut[i]);
}
else cut[i] = min(cut[pre]+i-pre, cut[i]);
}
}
}
int minCut(string s) {
vector<int> cut(s.length(), INT_MAX);
vector<vector<bool> > isPal(s.length(), vector<bool>(s.length(), false)); Parlindrome(isPal, s);
minCut(isPal, cut, s);
return cut[s.length()-];
}
};

leetcode 132: Palindrome Partitioning II

leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II的更多相关文章

  1. LeetCode 131. 分割回文串(Palindrome Partitioning)

    131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...

  2. Java实现 LeetCode 132 分割回文串 II(二)

    132. 分割回文串 II 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一 ...

  3. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  4. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  5. Leetcode之二分法专题-275. H指数 II(H-Index II)

    Leetcode之二分法专题-275. H指数 II(H-Index II) 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. ...

  6. Leetcode之回溯法专题-90. 子集 II(Subsets II)

    Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...

  7. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

  8. 前端与算法 leetcode 350. 两个数组的交集 II

    目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...

  9. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

随机推荐

  1. hdu 3646

    DP  状态转移方程还是比较容易想到  关键问题是当前要攻击的怪兽的血量 dp[i][j] = max(dp[i-1][j]+第i只鸟不使用double可杀死的怪兽数, dp[i-1][j-1]+第i ...

  2. uva 567

    Floyd 算法   就输入麻烦点 #include <iostream> #include <cstring> #include <cstdlib> #inclu ...

  3. Win32 DLL和MFC DLL 中封装对话框

    现在最常看见的关于DLL的问题就是如何在DLL中使用对话框,这是一个很普遍的关于如何在DLL中使用资源的问题.这里我们从Win32   DLL和MFC   DLL两个方面来分析并解决这个问题.     ...

  4. tomcat配置301重定向(urlRewrite URL重写)

    tomcat默认情况下不带www的域名是不会跳转到带www的域名的,而且也无法像apache那样通过配置.htaccess来实现.如果想要把不带“www'的域名重定向到带”www"域名下,又 ...

  5. RCC 2014 Warmup (Div. 2) ABC

    题目链接 A. Elimination time limit per test:1 secondmemory limit per test:256 megabytesinput:standard in ...

  6. POJ 3761 Bubble Sort(乘方取模)

    点我看题目 题意 : 冒泡排序的原理众所周知,需要扫描很多遍.而现在是求1到n的各种排列中,需要扫描k遍就变为有序的数列的个数,结果模20100713,当然了,只要数列有序就扫描结束,不需要像真正的冒 ...

  7. linux grep和正则表达式

    虽然正则表达式经常都在用,但是很少能够静下心来仔细的总结一下.最近看了一个台湾人的网站叫做鸟哥Linux私房菜,关于正则表达式的描述挺详细的.在此,我进行一下总结,如果想仔细的学习正则表达式,请访问鸟 ...

  8. MVC项目总结(别人的好文章)

    引用 http://www.cnblogs.com/xling/archive/2012/07/11/2587002.html

  9. Ubuntu中安装DiscuzX2

    http://blog.csdn.net/kevin_ysu/article/details/7452938 一.Apache的安装 Apache作为一个功能强大的Web程序,自然是架建Web服务器的 ...

  10. nginux做反向代理配置文件

    做反向代理的配置文件最好单独创建一个文件,然后在主配置文件中使用 include nginx-test.config;  这样的方式来导入. 配置代码如下: ## Basic reverse prox ...