当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

qt opengl 使用不同的颜色绘制线框三角形

QT添加资源文件、样式表、qss文件使用

【7.31】代码源 - 【矩阵操作】【宝箱】【New Stone Game】【等差数列】

部门之间,互不信任正常吗?(你是否遇到过)

Kubernetes:(八)调度约束和故障排查

【UE4】搭建局域网内VR直播 UE4.27

不想当Window的Dialog不是一个好Modal,弹窗翻身记...

服务器在线测速系统源码

Topic Modeling of Short Texts: A Pseudo-Document View

How does Excel compare if two columns of strings are the same?
随机推荐
怎么从零编写一个 v3 版本的 chrome 浏览器插件实现 CSDN 博客网站的暗黑和明亮主题切换?
Topic Modeling of Short Texts: A Pseudo-Document View
Fiddler基本使用
常用工具链和虚拟环境-TDMGCC
容联云发送验证码
【Arduino】重生之Arduino 学僧(3)----Arduino函数
可信的SSL证书颁发机构有哪些
openCV第一篇
MySQL-多表查询
Usage of permute() function in pytorch
VS2010 组件列表与对应名称
openCV第二篇
【云原生】灰度发布、蓝绿发布、滚动发布、灰度发布解释
Excel 如何比较两列字符串是否相同?
kubernetes部署ldap
自定义RunTimeException工具类
Summary of some interviews
五大靠谱的婚恋相亲APP详细特点缺点分析!
不想当Window的Dialog不是一个好Modal,弹窗翻身记...
复杂多层布局的初级智能文本提示器