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 分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的. 线段数+后缀数组代码如下: ...
随机推荐
- env.sh
#!/bin/sh#docker exec -i t nginx /usr/share/app/nginx/vip/webapp/evn.sh testtestUrl='http:\/\/192.16 ...
- 转发自AstralWind的博客(python正则表达式)
原文地址:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python ...
- linux C++ 共享库导出类
1.共享库的对外接口函数的声明必须加上extern “C”. 2.使用共享库对话接口函数生成的对象指针时在该对象未被释放之前不能关闭共享库句柄,否则会出现segmentation fault错误. 以 ...
- IOS照片框架
介绍 每天,用 iPhone 拍摄的照片数量超过了任何相机.每年 iOS 设备上的显示效果变得越来越好,回到 iPad 刚出现还没有 Retina 显示屏的时代,大屏幕的杀手级功能之一就是可以展示用户 ...
- Libgdx 开发指南(1.2) 应用框架——模块概览
模块概览 引言 LibGDX由一些为一个典型游戏架构中的各个步骤提供服务的模块组成. Input:为所有平台提供一致的输入模型与处理器.支持键盘.触屏.加速度传感器与鼠标. Graphics:使用硬件 ...
- 关于在listView中优化的问题 更多方
面试官 说 需要至少说出8种 (--) 1.convertView的复用,切忌每次getView()都新建.listView的核心原理就是重用view. 2.ViewHolder 减少findView ...
- Java-->利用URL类下载图片
--> 通过get 请求访问图片地址,将通过服务器响应的数据(即图片数据)存到本地文件中... --> HttpURLConnectionUtil 工具类 package com.drag ...
- YHLMR007 服装PDA验货差异报表
*********************************************************************** * Title : YHLMR007 * * Appli ...
- Codeforces Round #158 (Div. 2)
A. Adding Digits 枚举. B. Ancient Prophesy 字符串处理. C. Balls and Boxes 枚举起始位置\(i\),显然\(a_i \le a_j, 1 \l ...
- ipython
ipython 是一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多很有用的功能和函数. T ...