当前位置:网站首页>leetcode:140. 单词拆分 II
leetcode:140. 单词拆分 II
2022-08-03 02:04:00 【OceanStar的学习笔记】
题目来源
题目描述


题目解析
暴力递归
class Solution {
void process(string &s, std::set<std::string> &dict, int start, std::string &path, vector<string> &ans){
int N = s.size();
if(start == N){
ans.push_back(path);
return;
}
for (int end = start; end < N; ++end) {
std::string prefix = s.substr(start, end - start + 1);
if(dict.count(prefix)){
std::string tmp = path;
if(tmp.empty()){
path = prefix;
}else{
path = path.append(" ").append(prefix);
}
process(s, dict, end + 1, path, ans);
path = tmp;
}
}
}
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
std::set<std::string> dict(wordDict.begin(), wordDict.end());
vector<string> ans;
std::string path;
process(s, dict, 0, path, ans);
return ans;
}
};
优化:动态规划求是否有解,回溯算法搜索所有符合条件的解
class Solution {
struct Node{
std::string path;
bool end;
std::vector<Node *> nexts;
Node(){
end = false;
nexts.resize(26);
}
};
Node * getTrie(vector<string>& wordDict){
Node * root = new Node;
for(auto & word : wordDict){
Node *node = root;
for(char ch : word){
int idx = ch - 'a';
if(node->nexts[idx] == nullptr){
node->nexts[idx] = new Node;
}
node = node->nexts[idx];
}
node->path = word;
node->end = true;
}
return root;
}
std::vector<bool> getDP(string &s, Node *root){
int N = s.size();
std::vector<bool> dp(N + 1, false);
dp[N] = true;
for (int start = N - 1; start >= 0; --start) {
Node *curr = root;
for (int end = start; end < N; ++end) {
int idx = s[end] - 'a';
curr = curr->nexts[idx];
if(curr == nullptr){
break;
}
if(curr->end && dp[end + 1]){
dp[start] = true;
break;
}
}
}
return dp;
}
// str[index.....] 是要搞定的字符串
// dp[0...N-1] 0... 1.... 2... N-1... 在dp里
// root 单词表所有单词生成的前缀树头节点
// path str[0..index-1]做过决定了,做的决定放在path里
void process(string str, int idx, Node *root,
std::vector<bool> &dp, std::vector<string>& path,
std::vector<string> & ans){
if(idx == str.size()){
string builder;
for (int i = 0; i < path.size() - 1; ++i) {
builder = builder.append(path[i] + " ");
}
builder.append(path[path.size() - 1]);
ans.push_back(builder);
}else{
Node *curr = root;
for (int end = idx; end < str.size(); ++end) {
// str[i..end] (能不能拆出来)
int road = str[end] - 'a';
curr = curr->nexts[road];
if(curr == nullptr){
break;
}
if(curr->end && dp[end + 1]){
path.push_back(curr->path);
process(str, end + 1, root, dp, path, ans);
path.pop_back();
}
}
}
}
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
Node * tree = getTrie(wordDict);
std::vector<bool> dp = getDP(s, tree);
std::vector<string> path;
std::vector<string> ans;
process(s, 0, tree, dp, path, ans);
return ans;
}
};
边栏推荐
猜你喜欢

Wireshark data capture and analysis of the transport layer protocol (TCP protocol)

任意版本JLink驱动官方下载指引

236. The binary tree in recent common ancestor

10大领域5大过程47子过程快速记忆

Excel 如何比较两列字符串是否相同?

【云原生】阿里云ARMS业务实时监控

无法启动服务 错误 193 0xc1

Topic Modeling of Short Texts: A Pseudo-Document View

新库上线 | CnOpenDataA股上市公司董监高信息数据

visual studio 2012 为啥这么优秀
随机推荐
370万欧元!西班牙iPronics加速可重构光子芯片商用
MySQL-如何分库分表?一看就懂
openCV第二篇
工作两年成跳槽高峰期,程序员会在一家公司待多久?
二叉树的前序遍历、中序遍历、后序遍历和层序遍历
Usage of permute() function in pytorch
ROS计算图——rqt_graph
常用工具链和虚拟环境-TDMGCC
常见钓鱼手法及防范
新库上线 | CnOpenDataA股上市公司董监高信息数据
【UE4】Build VR live broadcast in LAN UE4.27
pytest:如何调用 pytest
PHICOMM(斐讯)N1盒子 - Armbian5.77(Debian 9)刷入EMMC
程序员写代码日常 | 每日趣闻
能添加任意贴图超级复布局的初级智能文本提示器
rancher集成ldap,实现统一账号登录
236. The binary tree in recent common ancestor
QT添加资源文件、样式表、qss文件使用
DTD约束和Schema约束
Incorrect datetime value: '2022-01-01' for function str_to_date