Leetcode: Palindrome Partition I II
思路
1. 第一遍做时就参考别人的, 现在又忘记了 做的时候使用的是二维动态规划, 超时加超内存
2. 只当 string 左部分是回文的时候才有可能减少 cut
3. 一维动规. 令 cuts[i] 表示string[i, string.size()] 所需的切割数, 那么
状态转移方程为 cuts[i] = min(cuts[j]+1) j > i && string[i, j] is palindrome
时间复杂度上仍是 o(n*n), 但更新 cuts 的限制条件比较多了, cuts[i] 更新频率较低
代码:
超时二维动规代码
#include <iostream>
#include <memory.h>
using namespace std; int cuts[1000][1000];
int palindrom[1000][1000];
const int INFS = 0x3f3f3f3f;
class Solution {
public:
int minCut(string s) {
memset(cuts, 0x3f, sizeof(cuts));
memset(palindrom, 0x3f, sizeof(palindrom)); int curcuts = countCuts(s,0,s.size()-1); return curcuts;
}
int countCuts(string &s, int i, int j) {
if(j <= i) return 0; if(isPalindrome(s,i,j))
return (cuts[i][j]=0); if(cuts[i][j] != INFS)
return cuts[i][j]; int curcuts = INFS;
for(int k = i; k < j; k++) {
curcuts = min(curcuts, 1+countCuts(s,i,k)+countCuts(s,k+1,j));
}
return (cuts[i][j]=curcuts);
} bool isPalindrome(string &s, int i, int j) {
if(palindrom[i][j] == 1)
return true;
if(j <= i)
return (palindrom[i][j] = true);
if(palindrom[i][j] == 0)
return false;
return (palindrom[i][j] = (s[i]==s[j] && isPalindrome(s,i+1,j-1)));
}
}; int main() {
string str = "apjesgpsxoeiokmqmfgvjslcjukbqxpsobyhjpbgdfruqdkeiszrlmtwgfxyfostpqczidfljwfbbrflkgdvtytbgqalguewnhvvmcgxboycffopmtmhtfizxkmeftcucxpobxmelmjtuzigsxnncxpaibgpuijwhankxbplpyejxmrrjgeoevqozwdtgospohznkoyzocjlracchjqnggbfeebmuvbicbvmpuleywrpzwsihivnrwtxcukwplgtobhgxukwrdlszfaiqxwjvrgxnsveedxseeyeykarqnjrtlaliyudpacctzizcftjlunlgnfwcqqxcqikocqffsjyurzwysfjmswvhbrmshjuzsgpwyubtfbnwajuvrfhlccvfwhxfqthkcwhatktymgxostjlztwdxritygbrbibdgkezvzajizxasjnrcjwzdfvdnwwqeyumkamhzoqhnqjfzwzbixclcxqrtniznemxeahfozp";
cout << str.size() << endl;
cout << (new Solution())->minCut(str) << endl;
return 0;
}
优化后的一维动规
#include <iostream>
#include <memory.h>
using namespace std; int cuts[1500];
int palindrom[1500][1500];
const int INFS = 0x3f3f3f3f;
class Solution {
public:
int minCut(string s) {
memset(cuts, 0x3f, sizeof(cuts));
memset(palindrom, 0x3f, sizeof(palindrom)); int curcuts = countCuts(s,0,s.size()-1); return curcuts;
}
int countCuts(string &s, int i, int j) {
if(j <= i) return 0; if(isPalindrome(s,i,j))
return 0; if(cuts[i] != INFS)
return cuts[i]; int curcuts = INFS; for(int k = i; k < j; k++) {
if(isPalindrome(s,i,k))
curcuts = min(curcuts, 1+countCuts(s,k+1,j));
}
return (cuts[i]=curcuts);
} bool isPalindrome(string &s, int i, int j) {
if(palindrom[i][j] == 1)
return true;
if(j <= i)
return (palindrom[i][j] = true);
if(palindrom[i][j] == 0)
return false;
return (palindrom[i][j] = (s[i]==s[j] && isPalindrome(s,i+1,j-1)));
}
}; int main() {
string str = "bb";
cout << str.size() << endl;
cout << (new Solution())->minCut(str) << endl;
return 0;
}
I
第一题用动态规划也是可以做的, 不过会比较麻烦(与Word Break类似)
这里用 dfs 加打印路径, 比较直观
int palindrom[1500][1500];
vector<vector<string> > res;
class Solution {
public:
vector<vector<string>> partition(string s) {
res.clear();
memset(palindrom, 0x3f, sizeof(palindrom));
vector<string> tmp;
dfs(s, tmp, 0);
return res; }
bool isPalindrome(string &s, int i, int j) {
if(palindrom[i][j] == 1)
return true;
if(j <= i)
return (palindrom[i][j] = true);
if(palindrom[i][j] == 0)
return false;
return (palindrom[i][j] = (s[i]==s[j] && isPalindrome(s,i+1,j-1)));
}
void dfs(string &s, vector<string> cur_vec, int depth) {
if(depth == s.size()) {
res.push_back(cur_vec);
return;
}
for(int i = depth; i < s.size(); i ++) {
if(isPalindrome(s, depth,i)) {
cur_vec.push_back(s.substr(depth,i-depth+1));
dfs(s, cur_vec, i+1);
cur_vec.pop_back();
}
}
}
};
Leetcode: Palindrome Partition I II的更多相关文章
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
- [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 I & II
Palindrome Permutation Given a string, determine if a permutation of the string could form a palindr ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- [LeetCode] Palindrome Partitioning 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 ...
- Palindrome Partitioning I & II
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode: Palindrome 回文相关题目
LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
随机推荐
- Ubuntu 14.04安装配置NFS
(一)安装NFS服务器 sudo apt-get install nfs-kernel-server sudo apt-get install nfs-common(在安装nsf-kernel-se ...
- MySQL学习总结(二)数据库以及表的基本操作
上一节中详细的介绍了关于MySQL数据库的安装过程,接下来我们就该对数据库以及表进行一些基本的操作了. 1.数据类型 MySQL数据库中提供了整数类型.浮点数类型.定点数类型.日期和时间类型.字符串类 ...
- bcdedit
我的电脑装了双系统:Win2003 SP2(C盘)和Win2008 SP2(D盘),最近2003一启动就蓝屏unknown hard error,安全模式也进不去,恢复注册表等方法试过也不行,但200 ...
- AutoFac文档5(转载)
目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 扫描 autofac可 ...
- 概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM 各公共网关接口介绍
CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...
- Junit运行在Spring环境下
@RunWith(SpringJUnit4ClassRunner.class)让测试运行于Spring测试环境 @ContextConfiguration 用来指定加载的Spring配置文件的位置,会 ...
- atitit.故障排除--- 当前命令发生了严重错误。应放弃任何可能产生的结果sql server 2008
atitit.故障排除--- 当前命令发生了严重错误.应放弃任何可能产生的结果sql server 2008 1. 现象 1 2. 原因:::sql server的bug 或者限制,查询的时候儿使用资 ...
- mac 写NTFS磁盘
最简单的方法就是把 OS X 自带的 mount_ntfs 默认加载方式从只读改成读写, 具体方法如下 # 用 root 身份做如下操作 (高危! 请切记自己在干什么)sudo -s cd /sbin ...
- 从调试角度理解ActionContext、OgnlContext、OgnlValueStack的关系
被调试代码: package web; import java.util.Map; import javax.servlet.http.HttpServletRequest; import or ...
- 每日英语:Is Bedtime Snacking Bad?
It's a familiar scenario in many households: Hours after dinner, the stomach growls and the refriger ...