I:

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"]
] 先切割,而后判断是否为回文。若是,则将剩余部分继续进行深度遍历。
 class Solution {
public:
vector<vector<string>> partition(string s) {
if(s.length()<) return res;
dfs(s,);
return res;
} void dfs(string s,int n){
int len=s.length();
if(n==len){
res.push_back(v);
return ;
}
for(int i=;i<len-n+;i++){
string tmp=s.substr(n,i);
if(isPalindrome(tmp)){
v.push_back(tmp);
dfs(s,n+i);
v.pop_back();
}
}
} bool isPalindrome(string s){
int n=s.length();
int i=,j=n-;
while(i<j){
if(s[i]!=s[j])
return false;
i++;
j--;
}
return true;
}
vector<string> v;
vector<vector<string>> res;
};

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.

从后往前构造二维数组isPalin,用于存储已经确定的回文子串。isPalin[i][j]==true代表s[i,...,j]是回文串。

在构造isPalin的同时使用动态规划计算从后往前的最小切分数,记录在min数组中。min[i]代表s[i,...,n-1]的最小切分数。

(上述两步分开做会使得代价翻倍,容易TLE)

关键步骤:

1、min[i]初始化为min[i+1]+1,即初始化s[i]与s[i+1]之间需要切一刀。这里考虑边界问题,因此min数组设为n+1长度。

2、从i到n-1中间如果存在位置j,同时满足:(1)s[i,...,j]为回文串;(2)1+min[j+1] < min[i]。

那么min[i]=1+min[j+1],也就是说一刀切在j的后面比切在i的后面要好。

 class Solution {
public:
int minCut(string s) {
int n=s.length();
if(n==) return ;
vector<vector<bool>> isPali(n,vector<bool>(n,false));
vector<int> min(n+,-);
for(int i=;i<n;i++){
isPali[i][i]=true;
}
for(int i=n-;i>=;i--){
min[i]=min[i+]+;
for(int j=i+;j<n;j++){
if(s[i]==s[j]){
if(j==i+||isPali[i+][j-]==true){
isPali[i][j]=true;
if(j==n-)
min[i]=;
else if(min[i]>min[j+]+)
min[i]=min[j+]+;
}
}
}
}
return min[];
}
};

palindrome-partitioning I&II——回文切割、深度遍历的更多相关文章

  1. 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

    131. Palindrome Partitioning Given a string s, partition s such that every substring of the partitio ...

  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 Partitioning I&II

    1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...

  4. 【算法】leetcode之 Palindrome Partitioning I&II(转载)

    1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...

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

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

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

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

  7. Palindrome(最长回文串manacher算法)O(n)

     Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  8. [LeetCode] Find the Closest Palindrome 寻找最近的回文串

    Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...

  9. Palindrome Partitioning I & II

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

随机推荐

  1. 剑指Offer(书):反转链表

    题目:输入一个链表,反转链表后,输出新链表的表头. 分析:要分清他的前一个节点和后一个节点,开始的时候前节点为null,后节点为head.next,之后,反转. public ListNode Rev ...

  2. hdu_2092_整数解

    枚举 #include <iostream> #include <cstdio> #include <cmath> using namespace std; int ...

  3. [BZOJ3611] [Heoi2014]大工程(DP + 虚树)

    传送门 $dp[i][0]$表示节点i到子树中的所有点的距离之和 $dp[i][1]$表示节点i到子树中最近距离的点的距离 $dp[i][2]$表示节点i到子树中最远距离的点的距离 建好虚树后dp即可 ...

  4. POJ 2914 Minimum Cut 全局最小割

    裸的全局最小割了吧 有重边,用邻接矩阵的时候要小心 #include<iostream> #include<cstdio> #include<bitset> #in ...

  5. 算法复习——网络流模板(ssoj)

    题目: 题目描述 有 n(0<n<=1000)个点,m(0<m<=1000)条边,每条边有个流量 h(0<=h<35000),求从点 start 到点 end 的最 ...

  6. 常州模拟赛d2t3 小X的佛光

    平日里最喜欢做的事就是蒸发学水.[题目描述]小 X 所在的城市 X 城是一个含有 N 个节点的无向图,同时,由于 X 国是一个发展中国家,为了节约城市建设的经费,X 国首相在建造 X 城时只建造 N ...

  7. 【二叉树】hdu 1622 Trees on the level

    [题意] 给定一棵树每个结点的权重和路径(路径用LR串表示),输出这棵树的层次遍历 [思路] 注意输入输出,sscanf用来格式化地截取需要的数据,strchr来在字符串中查找字符的位置 [Accep ...

  8. spring5响应式编程

    1.Spring5新特性    2.响应式编程响应式编程:非阻塞应用程序,借助异步和事件驱动还有少量的线程垂直伸缩,而非横向伸缩(分布式集群)当Http连接缓慢的时候,从数据库到Http数据响应中也会 ...

  9. OTOCI(bzoj 1180)

    Description 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出“no”.否则输出“yes ...

  10. PatentTips - Blending a Graphic over a Video

    Television broadcasts of events can be enhanced by providing graphics that are blended with other im ...