131. 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:
bool isPalindrome(string s)
{
int l = s.length(), left, right;
for(left = , right = l-; left < right; left++, right--)
{
if(s[left] != s[right])
return false;
}
return true;
} void partitionHelper(vector<vector<string>> &ans, string &s, int start, vector<string> &vec)
{
int l = s.length(), i;
if(start == l)
{
ans.push_back(vec);
return;
}
for(i = start; i < l; i++)
{
string sub = s.substr(start, i-start+);
if(isPalindrome(sub))
{
vec.push_back(sub);
partitionHelper(ans, s, i+, vec);
vec.pop_back();
}
}
} vector<vector<string>> partition(string s) {
vector<vector<string>> ans;
vector<string> vec;
partitionHelper(ans, s, , vec);
return ans;
}
};

132. 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:
int minCut(string s) {
int l = s.length(), i, j;
if(l <= )
return ;      //判断是否为回文字符串
vector<vector<bool>> isPal(l, vector<bool>(l, false));
for(i = l-; i >= ; i--) //HERE
{
isPal[i][i] = true;
for(j = i+; j < l; j++)
{
if(s[i] == s[j] && (j == i+ || isPal[i+][j-]))
isPal[i][j] = true;
}
}
vector<int> num(l);
num[] = ;
for(i = ; i < l; i++)
{
if(isPal[][i])
{
num[i] = ;
continue;
}
num[i] = i;
for(j = ; j <= i; j++)
{
if(isPal[j][i] && num[j-]+ < num[i])
num[i] = num[j-] + ;
}
}
return num[l-];
}
};

(1)

//construct the pailndrome checking matrix
// 1) matrix[i][j] = true; if (i==j) -- only one char
// 2) matrix[i][j] = true; if (i==j+1) && s[i]==s[j] -- only two chars
// 3) matrix[i][j] = matrix[i+1][j-1]; if s[i]==s[j] -- more than two chars

注意:

在构造矩阵时,要自下往上,否则一些位置会用到的值还没有填写。

(2)

/*
* Dynamic Programming
* -------------------
*
* Define res[i] = the minimum cut from 0 to i in the string.
* The result eventually is res[s.size()-1].
* We know res[0]=0. Next we are looking for the optimal solution function f.
*
* For example, let s = "leet".
*
* f(0) = 0; // minimum cut of str[0:0]="l", which is a palindrome, so not cut is needed.
* f(1) = 1; // str[0:1]="le" How to get 1?
* f(1) might be: (1) f(0)+1=1, the minimum cut before plus the current char.
* (2) 0, if str[0:1] is a palindrome (here "le" is not )
* f(2) = 1; // str[0:2] = "lee" How to get 2?
* f(2) might be: (1) f(1) + 1=2
* (2) 0, if str[0:2] is a palindrome (here "lee" is not)
* (3) f(0) + 1, if str[1:2] is a palindrome, yes!
* f(3) = 2; // str[0:3] = "leet" How to get 2?
* f(3) might be: (1) f(2) + 1=29
* (2) 0, if str[0:3] is a palindrome (here "leet" is not)
* (3) f(0) + 1, if str[1:3] is a palindrome (here "eet" is not)
* (4) f(1) + 1, if str[2:e] is a palindrome (here "et" is not)
* OK, output f(3) =2 as the result.
*
* So, the optimal function is:
*
* f(i) = min [ f(j)+1, j=0..i-1 and str[j:i] is palindrome
* 0, if str[0,i] is palindrome ]
*
* The above algorithm works well for the smaller test cases, however for the big cases, it still cannot pass.
* Why? The way we test the palindrome is time-consuming.
*
* Also using the similar DP idea, we can construct the look-up table before the main part above,
* so that the palindrome testing becomes the looking up operation. The way we construct the table is also the idea of DP.
*
* e.g. mp[i][j]=true if str[i:j] is palindrome.
* mp[i][i]=true;
* mp[i][j] = true if str[i]==str[j] and (mp[i+1][j-1]==true or j-i<2 ) j-i<2 ensures the array boundary.
*/

131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串的更多相关文章

  1. 132 Palindrome Partitioning II 分割回文串 II

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 符合要求的的最少分割次数.例如,给出 s = "aab",返回 1 因为进行一次分割可以将字符串 s 分 ...

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

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

  3. 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

  4. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

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

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

  6. Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)

    Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...

  7. 分割回文串 · Palindrome Partitioning

    [抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...

  8. lintcode:Palindrome Partitioning 分割回文串

    题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...

  9. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

随机推荐

  1. CentOS 7一些常用配置

    一.更改启动模式 背景:个人开发环境,安装了GNOME桌面,默认启动是图形化模式. 修改为命令行模式. systemctl set-default multi-user.target 二.命令行模式下 ...

  2. [C程序设计语言]第五部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. CUBRID学习笔记 8 复制数据库

    1  export  database  类似sqlserver的分离数据库 cubrid unloaddb demodb分离后生成三个文件 demodb_objects, demodb_indexe ...

  4. 2.mybatis入门实例 连接数据库进行查询

    1.新建项目,添加mybatis和mysql的jar包 2.在mysql中新建表user[id,name,age] CREATE TABLE `users` ( `id` ) NOT NULL aut ...

  5. 《NoSQL精粹》思维导图读书笔记

    <NoSQL精粹>思维导图读书笔记 各主题笔记 这本书短小精悍,虽不能解答所有NoSQL疑问,但在读书过程中会抛给你不少未曾想过的问题,给人以更深入的思考: 这里对每一个主题分别做了笔记: ...

  6. SQL 语句集合

    创建数据库 创建之前判断该数据库是否存在 if exists (select * from sysdatabases where name='databaseName') drop database ...

  7. Linux_常用命令_02

    1. 配置网络参数: (1).root登录 --> setup命令 进入到 "text mode setup utiliy" (2).运行命令"/etc/rc.d/ ...

  8. web设计经验<六>令网站看起来不专业的10个设计误区

    不管你是不是一个羽翼未丰企业的领导,专业的网站能为你带来的东西比你想象的多很多.退一万步来说,“考虑到我们是一个小厂”,粗糙的网站也许能被用户理解,但是不一定能接受.每天大家所浏览的大量的网站,已经从 ...

  9. placeholder在不同浏览器下的表现及兼容方法(转)

    1.什么是placeholder?    placeholder是html5新增的一个属性,当input或者textarea设置了该属性后,该值的内容将作为灰字提示显示在文本框中,当文本框获得焦点(或 ...

  10. Java源码初学_ArrayList

    一.ArrayList的构造器和构造方法 在ArrayList中定义了两个空数组,分别对应当指定默认构造方法时候,指向的数组已经给定容量,但是容量等于0的时候,指向的数组.此外在构造函数中传入Coll ...