LeetCode: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"]
]

分析:首先对字符串的所有子串判断是否是回文,设f[i][j] = true表示以i为起点,长度为j的子串是回文,等于false表示不是回文,那么求f[i][j]的动态规划方程如下:

  • 当j = 1,f[i][j] = true;

  • 当j = 2,f[i][j] = (s[i]==s[i+1]),其中s是输入字符串

  • 当j > 2, f[i][j] = f[i+1][j-2] && (s[i] == s[i+j-1])(即判断s[m..n]是否是回文时:只要s[m+1...n-1]是回文并且s[m] = s[n],那么它就是回文,否则不是回文)

这一题也可以不用动态规划来求f,可以用普通的判断回文的方式判断每个子串是否为回文。                                                                                                               本文地址

求得f后,根据 f 可以构建一棵树,可以通过DFS来枚举所有的分割方式,代码如下:

 class Solution {
public:
vector<vector<string>> partition(string s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector< vector<string> >res;
int len = s.length();
if(len == )return res;
//f[i][j] = true表示以i为起点,长度为j的子串是回文
bool **f = new bool*[len];
for(int i = ; i < len; i++)
{
f[i] = new bool[len+];
for(int j = ; j < len+; j++)
f[i][j] = ;
f[i][] = true;
}
for(int k = ; k <= len; k++)
{
for(int i = ; i <= len-k; i++)
{
if(k == )f[i][] = (s[i] == s[i+]);
else f[i][k] = f[i+][k-] && (s[i] == s[i+k-]);
}
}
vector<string> tmp;
DFSRecur(s, f, , res, tmp);
for(int i = ; i < len; i++)
delete [](f[i]);
delete []f;
return res;
} void DFSRecur(const string &s, bool **f, int i,
vector< vector<string> > &res, vector<string> &tmp)
{//i为遍历的起点
int len = s.length();
if(i >= len){res.push_back(tmp); return;}
for(int k = ; k <= len - i; k++)
if(f[i][k] == true)
{
tmp.push_back(s.substr(i, k));
DFSRecur(s, f, i+k, res, tmp);
tmp.pop_back();
} }
};

LeetCdoe: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.                                                                                                          本文地址

算法1:在上一题的基础上,我们很容易想到的是在DFS时,求得树的最小深度即可(遍历时可以根据当前求得的深度进行剪枝),但是可能是递归层数太多,大数据时运行超时,也贴上代码:

 class Solution {
public:
int minCut(string s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int len = s.length();
if(len <= )return ;
//f[i][j] = true表示以i为起点,长度为j的子串是回文
bool **f = new bool*[len];
for(int i = ; i < len; i++)
{
f[i] = new bool[len+];
for(int j = ; j < len+; j++)
f[i][j] = ;
f[i][] = true;
}
for(int k = ; k <= len; k++)
{
for(int i = ; i <= len-k; i++)
{
if(k == )f[i][] = (s[i] == s[i+]);
else f[i][k] = f[i+][k-] && (s[i] == s[i+k-]);
}
}
int res = len, depth = ;
DFSRecur(s, f, , res, depth);
for(int i = ; i < len; i++)
delete [](f[i]);
delete []f;
return res - ;
}
void DFSRecur(const string &s, bool **f, int i,
int &res, int &currdepth)
{
int len = s.length();
if(i >= len){res = res<=currdepth? res:currdepth; return;}
for(int k = ; k <= len - i; k++)
if(f[i][k] == true)
{
currdepth++;
if(currdepth < res)
DFSRecur(s, f, i+k, res, currdepth);
currdepth--;
} } };

算法2:设f[i][j]是i为起点,长度为j的子串的最小分割次数,f[i][j] = 0时,该子串是回文,f的动态规划方程是:

f[i][j] = min{f[i][k] + f[i+k][j-k] +1} ,其中 1<= k <j

这里f充当了两个角色,一是记录子串是否是回文,二是记录子串的最小分割次数,可以结合上一题的动态规划方程,算法复杂度是O(n^3), 还是大数据超时,代码如下:

 class Solution {
public:
int minCut(string s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int len = s.length();
if(len <= )return ;
//f[i][j] = true表示以i为起点,长度为j的子串的最小切割次数
int **f = new int*[len];
for(int i = ; i < len; i++)
{
f[i] = new int[len+];
for(int j = ; j < len+; j++)
f[i][j] = len;
f[i][] = ;
}
for(int k = ; k <= len; k++)
{
for(int i = ; i <= len-k; i++)
{
if(k == && s[i] == s[i+])f[i][] = ;
else if(f[i+][k-] == &&s[i] == s[i+k-])f[i][k] = ;
else
{
for(int m = ; m < k; m++)
if(f[i][k] > f[i][m] + f[i+m][k-m] + )
f[i][k] = f[i][m] + f[i+m][k-m] + ;
}
}
}
int res = f[][len], depth = ;
for(int i = ; i < len; i++)
delete [](f[i]);
delete []f;
return res;
}
};

算法3:同上一题,用f来记录子串是否是回文,另外优化最小分割次数的动态规划方程如下,mins[i] 表示子串s[0...i]的最小分割次数:

  • 如果s[0...i]是回文,mins[i] = 0
  • 如果s[0...i]不是回文,mins[i] = min{mins[k] +1 (s[k+1...i]是回文)  或  mins[k] + i-k  (s[k+1...i]不是回文)} ,其中0<= k < i

代码如下,大数据顺利通过,结果Accept:                                                                                                                                 本文地址

 class Solution {
public:
int minCut(string s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int len = s.length();
if(len <= )return ;
//f[i][j] = true表示以i为起点,长度为j的子串是回文
bool **f = new bool*[len];
for(int i = ; i < len; i++)
{
f[i] = new bool[len+];
for(int j = ; j < len+; j++)
f[i][j] = false;
f[i][] = true;
}
int mins[len];//mins[i]表示s[0...i]的最小分割次数
mins[] = ;
for(int k = ; k <= len; k++)
{
for(int i = ; i <= len-k; i++)
{
if(k == )f[i][] = (s[i] == s[i+]);
else f[i][k] = f[i+][k-] && (s[i] == s[i+k-]);
}
if(f[][k] == true){mins[k-] = ; continue;}
mins[k-] = len - ;
for(int i = ; i < k-; i++)
{
int tmp;
if(f[i+][k-i-] == true)tmp = mins[i]+;
else tmp = mins[i]+k-i-;
if(mins[k-] > tmp)mins[k-] = tmp;
}
}
for(int i = ; i < len; i++)
delete [](f[i]);
delete []f;
return mins[len-];
} };

 【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3421283.html

LeetCode:Palindrome Partitioning,Palindrome Partitioning II的更多相关文章

  1. [LeetCode] 680. Valid Palindrome II 验证回文字符串 II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  2. [LeetCode] 214. Shortest Palindrome 最短回文串

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. LeetCode 1216. Valid Palindrome III

    原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/ 题目: Given a string s and an integer k, f ...

  4. [LeetCode] 409. Longest Palindrome 最长回文

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  5. leetcode bug & 9. Palindrome Number

    leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...

  6. leetcode 第九题 Palindrome Number(java)

    Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...

  7. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

  8. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  9. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

随机推荐

  1. WebService学习总结(二)——WebService相关概念介绍

    一.WebService是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨平台的规范(抽象) 3. 多个跨平台.跨语言的应用间通信整合的方案(实际 ...

  2. Effective Java 27 Favor generic methods

    Static utility methods are particularly good candidates for generification. The type parameter list, ...

  3. Effective Java 74 Implement Serializable judiciously

    Disadvantage of Serializable A major cost of implementing Serializable is that it decreases the flex ...

  4. Android开发学习总结(一)——搭建最新版本的Android开发环境

    Android开发学习总结(一)——搭建最新版本的Android开发环境(转) 最近由于工作中要负责开发一款Android的App,之前都是做JavaWeb的开发,Android开发虽然有所了解,但是 ...

  5. redis 优化

    系统优化echo "vm.overcommit_memory=1" > /etc/sysctl.conf 0, 表示内核将检查是否有足够的可用内存供应用进程使用:如果有足够的 ...

  6. A daemon process class in python

    In everbright task schedule project, we need some daemon process to do certain work, here is a examp ...

  7. 详解apache的allow和deny

    今天看了一篇关于apache allow,deny的文章收获匪浅,防止被删,我直接摘过来了,原文地址!!! !http://www.cnblogs.com/top5/archive/2009/09/2 ...

  8. 自定义input[type="radio"]的样式

    对于表单,input[type="radio"] 的样式总是不那么友好,在不同的浏览器中表现不一. 为了最大程度的显示出它们的差别,并且为了好看,首先定义了一些样式: <fo ...

  9. 【CSS】颜色码对照表

    英文代码 形像颜色 HEX格式 RGB格式 LightPink 浅粉色 #FFB6C1 255,182,193 Pink 粉红 #FFC0CB 255,192,203 Crimson 猩红 #DC14 ...

  10. Windows事件ID大全

    51 Windows 无法找到网络路径.请确认网络路径正确并且目标计算机不忙或已关闭.如果 Windows 仍然无法找到网络路径,请与网络管理员联系. 52 由于网络上有重名,没有连接.请到“控制面板 ...