由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Palindrome Partitioning II 的DP做法?
相关主题
Palindrome Partitioning II Runtime Error请教一个leetcode time complexity,Palindrome Partitioning
请教一个Palindrome Partition问题Leetcode Scramble String简单解法
请教一下palindrome partitioning用memoization的问题Partition List的例子对吗?
palindrome partitioning 2palindrome partioning II
leetcode Palindrome PartitioningLongest Palindromic Substring 用 vector 超时
leetcode里的Palindrome partition问题这种backtracking的问题怎么算时间复杂度?比如palindrom patitioning.
大家帮忙看看我的Palindrome II 的解法C++ 程序求助
问问 leetcode 新题问个题
相关话题的讨论汇总
话题: cut话题: palindrome话题: int话题: dp话题: return
进入JobHunting版参与讨论
1 (共1页)
a***e
发帖数: 413
1
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.
我刚开始看DP,大概有点idea,但怎么都做不对。有同学知道下面这个double loop 到
底怎么错了吗?有什么好的学习DP的方法分享吗?多谢!
class Solution {
public:
int minCut(string s) {
int n=s.size();
if (n<=1) return 0;

vector> b(n, vector(n,false));
int cut[n+1];
for (int i=0; i<=n; i++)
{
cut[i]=i-1;
}

for (int i=0; i {
for (int j=i; j>=0; j--)
{
if (s[i]==s[j]&&(i-j<2||b[i+1][j-1]))
{
b[i][j]=true;
cut[i]=min(cut[i],cut[j+1]+1);
}
}
}
return cut[n];

}
};
a*****a
发帖数: 46
2
改成这样就可以了
if (s[i]==s[j]&&(i-j<2||(b[i-1][j+1])))
{
b[i][j]=true;
cut[i+1]=min(cut[i+1],cut[j]+1);
}

is

【在 a***e 的大作中提到】
: 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.
: 我刚开始看DP,大概有点idea,但怎么都做不对。有同学知道下面这个double loop 到
: 底怎么错了吗?有什么好的学习DP的方法分享吗?多谢!
: class Solution {
: public:

a***e
发帖数: 413
3
多谢大牛,这类题得多做才能积累经验是吗?总觉得状态转移方程很晕。
1 (共1页)
进入JobHunting版参与讨论
相关主题
问个题leetcode Palindrome Partitioning
求教leetcode上Palindrome Partitioning DFS解法的复杂度leetcode里的Palindrome partition问题
Palindrome Partitioning II双dp常见吗?大家帮忙看看我的Palindrome II 的解法
遍寻OJ的答案到处都没有,不知道大牛可以不可以发个自己的答案问问 leetcode 新题
Palindrome Partitioning II Runtime Error请教一个leetcode time complexity,Palindrome Partitioning
请教一个Palindrome Partition问题Leetcode Scramble String简单解法
请教一下palindrome partitioning用memoization的问题Partition List的例子对吗?
palindrome partitioning 2palindrome partioning II
相关话题的讨论汇总
话题: cut话题: palindrome话题: int话题: dp话题: return