[leetcode][回溯][DP] Palindrome Partitioning //TODO
2015-06-30 09:38
309 查看
题目:
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 =
Return
DP
注:效率还是不太高呀
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) { vector<vector<string> > res; vector<string> oneCombination; partitionCore(s, res, oneCombination); return res; } private: bool isPalindrome(string s){ int iBeg = 0, iEnd = s.size() - 1; while (iBeg < iEnd){ if (s[iBeg] != s[iEnd]) return false; ++iBeg; --iEnd; } return true; } void partitionCore(string s, vector<vector<string>> &res, vector<string> oneCombination){ if (s.empty() && !oneCombination.empty()){ res.push_back(oneCombination); return; } //查找前n个字符可以构成palindrome,然后递归对后半部分求解partition for (int i = 0; i < s.size(); ++i){ string sSubstr = s.substr(0, i+1); if (isPalindrome(sSubstr)){ oneCombination.push_back(sSubstr); partitionCore(s.substr(i + 1), res, oneCombination); oneCombination.pop_back(); } } } };
DP
void partitionCore(string s, int start, vector<vector<bool> > &table, vector<vector<string> > &res, vector<string> oneCombination){ if (start >= s.size() || table.empty()){ if (!oneCombination.empty()) res.push_back(oneCombination); return; } //查找前n个字符可以构成palindrome,然后递归对后半部分求解partition for (int i = start; i < s.size(); ++i){ string sSubstr = s.substr(start, i - start + 1); if (i == start || s[i] == s[start] && (i == start+1 || table[start + 1][i - 1])){ oneCombination.push_back(sSubstr); partitionCore(s, i + 1, table, res, oneCombination); oneCombination.pop_back(); } } } vector<vector<string> > partition(string s) { //table[i][j]表示s[i]...s[j]是否可以构成回文,是的充分必要条件是s[i]==s[j] && s[i+1]...s[j-1]构成回文 vector<bool> dummy(s.size()); vector<vector<bool> > table(s.size()); for (int i = 0; i < s.size(); ++i){ table[i] = dummy; } //table初始化 for (int i = 0; i < s.size(); ++i){ table[i][i] = true; } for (int i = s.size() - 1; i >= 0; --i){ for (int j = i + 1; j < s.size(); ++j){ if (s[i] == s[j] && (j == i + 1 || table[i + 1][j - 1])) table[i][j] = true; else table[i][j] = false; } } vector<vector<string> > res; vector<string> oneCombination; partitionCore(s, 0, table, res, oneCombination); return res; }
注:效率还是不太高呀
相关文章推荐
- Devexpress 实现插入已复制列
- 关于Linux和Windows文件路径的解决办法
- Linux进程间通信-信号量
- Apache Shiro 使用手册(四)Realm 实现
- Qt事件和事件循环
- 抽象工厂模式
- WindowsServer 2008 Ad建立ftp隔离用户
- 4.19构建一个类book,其中含有两个私有数据成员qu和price,建立一个有5个元素的数组对象,将初始化为1~5,将price初始化为qu的10倍1.显示每个对象的qu*price.
- HMC5883L常见问题解答
- 编写Android.mk中的LOCAL_SRC_FILES的终极技巧
- IDA Pro分析Android软件
- Python定制类
- tools save the logs during su procedure
- 查询oracle表的信息(表,字段,约束,索引)按列名+表名查询约束名称
- 黑马程序员——File类
- Linux System Programming note 8 ——File and Directory Management
- poj 2586 Y2K Accounting Bug
- 如何加强云端的SSH安全性 TechTarget中国原创内容,原文链接:http://www.searchcl
- Machine Learning---决策树
- GC、(三) GC Algorithms: Basics