Palindrome Partitioning I 和II的代码和理解(动态规划思想)
来源:互联网 发布:linux内核调试ida 编辑:程序博客网 时间:2024/06/08 15:06
题目1 :Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning ofs. For example, given s ="aab",
[ ["aa","b"], ["a","a","b"] ]
题目2:
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 ofs.
For example, given s ="aab", Return1since the palindrome partitioning["aa","b"]could be produced using 1 cut.
题目1给出两个解法:都是DFS深度优先搜索(用递归),第一个是我写的,后来看第二个想法特别简介,也写了一次。
1(被注释的):主函数先构造一个 dp[i][j] ,含义是从i到j的字符范围内是不是回文序列,是的话true,反义false ,它是一个半边的方阵。然后主函数中循环dp[i]中为true的,含义是从第一个字符开始的回文都遍历一遍。另一个函数dfspart()中先判断start达没达到总串s的长度,达到了就把一组答案tmp压入结果result中,然后返回。start记录的是一个位置,start前面的回文已经判断完了,从start向后找回文,还是循环dp[start]中为true的位置,再次用dfs函数找后一个位置。
2:第二个解法思想还是一样的,好处1是每次输入的s不是整个串了,而是把前面已经判断过的回文截断,(上一个解法是用总串s和start来解决这个问题)。好处2是不用先构造dp了,在dfs内用一个begin和end来判断部分子串是不是回文,是的话再用dfs遍历下一段s,不是的话就不用管它了。
class Solution {public: /* vector<vector<string>> partition(string s) { vector<vector<string> > result; int n=s.length(); vector<vector<bool> > dp(n,vector<bool>(n,false)); for(int i=0;i<n;++i){ for(int j=i;j<n;++j){ string part=s.substr(i,j-i+1); string partcopy=part; reverse(part.begin(),part.end()); if(part==partcopy) dp[i][j]=true; } } for(int i=0;i<n;i++){ if(dp[0][i]){ vector<string> tmp; tmp.push_back(s.substr(0,i+1)); dfspart(result,dp,i+1,tmp,s); } } return result; } void dfspart(vector<vector<string> > &result,vector<vector<bool> > &dp, int start,vector<string> &tmp,string &s){ if(start>=s.length()){ result.push_back(tmp); return; } for(int i=start;i<s.length();++i){ if(dp[start][i]){ tmp.push_back(s.substr(start,i-start+1)); dfspart(result,dp,i+1,tmp,s); tmp.pop_back(); } } } */ void dfs(string s,vector<vector<string>> &result,vector<string> &path){ if(s.length()==0) { result.push_back(path); return; } for(int i=0;i<s.length();i++){ int begin=0; int end=i; while(begin<end){ if(s[begin]==s[end]){ begin++;end--; }else break; } if(begin>=end){ path.push_back(s.substr(0,i+1)); dfs(s.substr(i+1),result,path); path.pop_back();//注意是用的path引用,要弹出一个 } } } vector<vector<string>> partition(string s) { vector<vector<string>> result; vector<string> path; dfs(s,result,path); return result; }};
题目2也写了两个解法,第一个被注释掉的就是题目1的方法,但是不能通过超时了,第二个是动态规划思想。先构造个dp[][],和上文的含义一样,不过构造方式我借用了别人的写法,很巧妙,也类似于动态规划,不用像之前的写法每次判断一下子串是不是回文,再赋值为true或flase。再构造个count ,其实这个count的逐步构造才是这个题最根本的动态规划思想,具体的解释写在注释里了。
class Solution {public: /* 内存超限 int minCut(string s) { vector<vector<string>> result; vector<string> path; dfs(s,result,path); int min=result[0].size(); for(int i=1;i<result.size();++i){ if(result[i].size()<min){ min=result[i].size(); } } return min-1; } void dfs(string s,vector<vector<string>> &result,vector<string> &path){ if(s.length()==0) { result.push_back(path); return; } for(int i=0;i<s.length();i++){ int begin=0; int end=i; while(begin<end){ if(s[begin]==s[end]){ begin++;end--; }else break; } if(begin>=end){ path.push_back(s.substr(0,i+1)); dfs(s.substr(i+1),result,path); path.pop_back(); } } } */ int minCut(string s) { int n=s.length(); if (0==n) return 0; vector<vector<bool>> dp(n,vector<bool>(n,false)); vector<int> count(n+1); count[0]=0; count[1]=1; for(int j=0;j<n;++j) for(int i=0;i<=j;++i){ dp[i][j]=(s[i]==s[j])&&( j-i<=2 || dp[i+1][j-1] ); //从i到j是不是回文,必须外循环是j }//例如,5个字符的最小分割数量取决于:<前4个字符的最小分割数量+1>或者//<后两个是不是回文:(不是的话不用管它)是的话,前3个字符的最小分割数量+1> 或者//<后三个是不是回文:(不是的话不用管它)是的话,前2个字符的最小分割数量+1> 或者//<后四个是不是回文:(不是的话不用管它)是的话,前1个字符的最小分割数量+1> 或者//<后五个是不是回文:(不是的话不用管它)是的话,前0个字符的最小分割数量+1>
//(这里前0个字符的最小分割是0,前1个字符的最小分割是1)初始化的 可以理解吧
//注意上边的ij是s的下标是s[i] s[j]表示位置。//注意下面的ij是第i个第j个字符,第0个其实是不存在的,对应count[0]=0,代表前0个字符的最小分割数量是0 for(int i=2;i<n+1;++i){ count[i]=count[i-1]+1;//<前i-1个字符的最小分割数量+1> for(int j=i-1;j>=1;--j){ if(dp[j-1][i-1]){ count[i]=min(count[j-1]+1,count[i]); }else{ //这里对应上边说的(不是的话不用管它) } } } return count[n]-1;//分割数量-1是切了几刀 }};
- Palindrome Partitioning I 和II的代码和理解(动态规划思想)
- Word Break I,II, Triangle,Palindrome Partitioning 动态规划 DP
- 「动态规划」Palindrome Partitioning II
- LeetCode 132 Palindrome Partitioning II (动态规划)
- 132. Palindrome Partitioning II (动态规划)
- Palindrome Partitioning I && II
- Palindrome Partitioning I II
- Palindrome Partitioning I,II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- leetcode题目:Palindrome Partitioning 和Palindrome Partitioning II
- LeetCode 之 Palindrome Partitioning II(动态规划)
- leetcode之palindrome-partitioning-ii(动态规划)
- 每日AC-LeetCode-palindrome-partitioning-ii --动态规划
- Leetcode: Palindrome Partitioning I & II
- leetcode Palindrome Partitioning I II
- 【LeetCode】Palindrome Partitioning I&II
- 【LeetCode】Palindrome Partitioning I && II
- LeetCode - Palindrome Partitioning I && II
- 自己动手写PHP框架(一)
- 向MOSS页面中添加服务器端代码的另外一种方式 zt
- 基于BASYS 2开发板的多功能数字钟Verilog程序
- 在MOSS中直接嵌入ASP.NET Page zt
- SharePoint Portal Server 2003 中的单一登录 zt
- Palindrome Partitioning I 和II的代码和理解(动态规划思想)
- 在moss上自己总结了点小经验。。高手可以飘过 转贴
- vs2008 开发 MOSS 顺序工作流
- 流媒体协议学习
- 5-1 最大子列和问题(思路)*
- 状态机工作流的 SpecialPermissions
- 第十一单元练习
- VS2008开发MOSS工作流几个需要注意的地方
- MOSS中如何自定义WebService