19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
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"]
] 思想: 简单的深度优先搜索。
bool isPalindrome(string& s, int l, int r) {
while(l++ < r--)
if(s[l] != s[r]) return false;
return true;
}
class Solution {
public:
void dfs(string& s, vector<string>& vec2, size_t id) {
if(id == s.size()) {
vec.push_back(vec2);
return;
}
for(int end = id; end < s.size(); ++end) {
if(isPalindrome(s, id, end)) {
vec2.push_back(s.substr(id, end-id+1));
dfs(s, vec2, end+1);
vec2.pop_back();
}
}
}
vector<vector<string> > partition(string s) {
if(s == "") return vec;
vector<string> vec2;
dfs(s, vec2, 0);
return vec;
}
private:
vector<vector<string> > vec;
};
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.
思想: 动态规划:
n = s.length();
Record[i] = 0 , ( i = n || isPalindrome(i, n-1))
min(n-1-i, Record[k]+1 ( isPalindrome(i, k) ) ) , otherwise
where i belong to interval [0, n].
class Solution {
public:
int minCut(string s) {
if(s == "" || s.size() == 1) return 0;
int n = s.size();
vector<vector<bool> > D(n, vector<bool>(n, false)); vector<int> record(n, 0);
for(int i = n-1; i >= 0; --i) {
record[i] = n-1-i;
for(int j = i; j < n; ++j) {
if(s[i] == s[j] && (j-i < 2 || D[i+1][j-1])) {
D[i][j] = true;
if(j == n-1) record[i] = 0;
else record[i] = min(record[i], record[j+1]+1);
}
}
}
return record[0];
}
};
19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)的更多相关文章
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【CF906E】Reverses(回文自动机,最小回文分割)
题意:给定两个长度相等的仅由小写字母组成的串A和B,问在A中最少选择多少段互不相交的子串进行翻转能使A和B相同 len<=5e5 思路:构造新串S=a[1]b[1]a[2]b[2]...a[n] ...
- Palindrome Partitioning LightOJ - 1044(回文串最小分割数,O(n^2)预处理子串是否回文)
题意:将一个字符串分割成最少的字符串,使得分割出的每个字符串都是回文串.输出最小的分割数. 方法(自己的):先O(n^2)(用某个点或某个空区间开始,每次向左右扩展各一个的方法)处理出所有子串是否回文 ...
- [Leetcode] palindrome partition ii 回文分区
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- PAT A1136 A Delayed Palindrome (20 分)——回文,大整数
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- codeforces 486C Palindrome Transformation 贪心求构造回文
点击打开链接 C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes ...
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
- Palindrome - URAL - 1297(求回文串)
题目大意:RT 分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的. 线段数+后缀数组代码如下: ...
随机推荐
- SoapUI API + Groovy API + Difference with Java
用soapUI进行webservice测试过程中,必不可少的要用到soapUI封装的代码.我们一起学习吧:) SoapUI 5.1.2 API:http://www.soapui.org/apidoc ...
- 网站后台调用winform MessageLoopApartment
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C++虚函数的实现机制示例
C++虚函数的实现机制是通过一个vtable表,指向子类的虚函数地址. 另外,如果不是虚函数,则不能实现用父类引用调用子类方法. #include <windows.h> #include ...
- C++ using namespace std(转载)
转载自http://www.kuqin.com/language/20080107/3532.html 感谢这位大神的解答! 以下的内容摘抄自转载的文章里面的部分内容. 早些的实现将标准库功能定义在全 ...
- Javascript DOM基础(二) childNodes、children
childNodes知识点: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Typ ...
- 将Controller抛出的异常转到特定View
<!-- 将Controller抛出的异常转到特定View --> <bean class="org.springframework.web.servlet.handler ...
- ODOO从哪里开始??OpenERP的第一根线头儿
Windows下ODOO源码启动: python odoo-bin -w odoo -r odoo --addons-path=addons,../mymodules --db-filter=mydb ...
- [dijkstra+heap优化] 模板
var n,m,s,i,j,x,y,z,l,tot :longint; pre,last,other,len :..] of longint; heap,d,pl :Array[..] of long ...
- ps颜色模式
HSB(hue.saturation.bright) 基于人眼 RGB 基于光 CMYK 基于色 LAB 基于大自然颜色库(理论)
- HighCharts使用心得
HighCharts使用心得 前言: 之前很早的一个项目中使用过highcharts,感觉挺方便的,图表类型也比较丰富,而且还支持数据的下钻,但是如果投入商业使用的话还会有一些版权的问题,所以后来就使 ...