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 分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的. 线段数+后缀数组代码如下: ...
随机推荐
- 高性能Mysql
高性能 高可用 易伸缩 安全 高并发 定义 吞吐量(单位时间内多少次查询) MTBF(平均失效时间) MTTR(平均恢复时间) 同时工作的线程数或连接数 系统 事务 表锁.行级 ...
- matlab 非平稳变化时域分析
对于非平稳信号,由于傅立叶变换核心函数-正弦函数具有无限性,因此选用短时窗来分析局域信号: 需要注意的时,选取完滑动的时间窗一般是中心对称而且为奇数,这时被分析的时间点正好是滑动窗的中点. 因此,时域 ...
- JVM-并发-线程
线程 1.线程的实现 (1)实现线程主要有3中方式:使用内核线程实现,使用用户线程实现和使用用户线程加轻量级进程混合实现. (2)使用内核线程实现 内核线程就是直接由操作系统内核支持的线程,这种线程由 ...
- Mac下同时安装多个版本的JDK & Mac 可设置环境变量的位置、查看和添加PATH环境变量
http://ningandjiao.iteye.com/blog/2045955 http://elf8848.iteye.com/blog/1582137
- Request.Form()的使用
在CS文件中获得对应页面中的下拉框DropDownList_sitebranch值可以有以下几种方法获得: siteInfo.FZJGID = DropDownList_sitebra ...
- python模块之time
Python中的时间模块. 1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素. 2.UTC(Coordinated U ...
- brute-force search
#include <pcl/search/brute_force.h> #include <pcl/common/common.h> #include <iostream ...
- lua class(table)
自己看吧: Base = {x = 0,y = 0} ---原型表 Base.name = "luohai"Base.age = 22Base.sex = "man&qu ...
- Intent 四个重要属性
Intent作为联系各Activity之间的纽带,其作用并不仅仅只限于简单的数据传递.通过其自带的属性,其实可以方便的完成很多较为复杂的操作.例如直接调用拨号功能.直接自动调用合适的程序打开不同类型的 ...
- hadoop删除节点。
hadoop节点摘除操作: 1.确定exclude文件的位置. <property> <name>dfs.hosts.exclude</name> <valu ...