题目一, 题目二

思路

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的更多相关文章

  1. LeetCode: Palindrome Partition

    LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...

  2. [Leetcode] palindrome partition ii 回文分区

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

  3. [LeetCode] Palindrome Permutation I & II

    Palindrome Permutation Given a string, determine if a permutation of the string could form a palindr ...

  4. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  5. [LeetCode] Palindrome Partitioning II 解题笔记

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

  6. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  7. Palindrome Partitioning I & II

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

  8. LeetCode: Palindrome 回文相关题目

    LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...

  9. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

随机推荐

  1. Linux中内存挂载到目录下

    [日期:2012-11-14]   /dev/shm是linux下的一块共享内存结构.默认大小是真实内存的一半.它用来存储进程间通讯时的一些共享数据结构.在物理内存足够时,会在内存中进行数据交换,如果 ...

  2. tomcat做成服务

    如果Tomcat是安装版的话服务就已经有了,那么解压版的tomcat(我就经常这么做)没服务怎么办了?     手动把tomcat做成服务方法:      1.环境变量配置jdk      2.运行c ...

  3. FFmpeg命令添加视频字幕

    FFmpeg添加字幕 首先需要科普下.vob,mkv等格式文件以流的形式存储字幕,而mp4不支持这种方式.如果希望生成带字幕的mp4文件,只能将字幕“烧录”到视频中. 也就是说我们需要将字幕流与视频流 ...

  4. 老生常谈combobox和combotree模糊查询

    FIRST /** * combobox和combotree模糊查询 * combotree 结果显示两级父节点(手动设置数量) * 键盘上下键选择叶子节点 * 键盘回车键设置文本的值 */ (fun ...

  5. php比较函数,判断安全函数

    一.字符串比较函数: int strcasecmp ( string $str1 , string $str2 ) int strcmp ( string $str1 , string $str2 ) ...

  6. Android 利用fastjson进行json解析

    package com.example.FastJson.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.Typ ...

  7. [na]华为acl(traffic-filter)和dhcp管理

    这个是财务网络的一个问题, 要求财务的某台机器能访问其他部门区的打印机. 其他部门是不能访问到财务网络的. 华为alc配置实例:-traffic-filter # 在VLAN100上配置基于ACL的报 ...

  8. 开源项目AndroidUtil-採用Fragment实现TabHost

    原文出自:方杰|http://fangjie.sinaapp.com/?p=141 转载请注明出处 学习Android也有一段时间了.感觉大部分的Android应用都有非常多类似的组件,所以就打算做了 ...

  9. 李洪强iOS开发之苹果企业开发者账号申请流程

    李洪强iOS开发之苹果企业开发者账号申请流程 一. 开发者账号类型选择 邓白氏码 DUNS number,是Data Universal Numbering System的缩写,是一个独一无二的9位数 ...

  10. jsp error-page没有生效

    1.首页检查web.xml中的配置,确保路径是正确的 <error-page> <error-code>404</error-code> <location& ...